jeudi 1 décembre 2016

C++ Finding the Length of an Array with a Pointer

The point of this program is to reverse a string and print it out. It is a school assignment and I'm just starting it. I need to reverse the string that was given to me (the whole main function was given for the assignment) and I need to do it by using 2 pointers for the beginning of the cstring and the end of the cstring. In the code, I know that the beginning of the cstring that is passed would be just "temp" but I don't know how I could find the ending letter of the cstring and assign a pointer to it.

The main function was given to me:

#include <iostream>
#include "reverse.hpp"

using namespace std;
void reverse(char*);

int main()
{
    // these are the test cases
    char str1[] = "time";
    char str2[] = "straw";
    char str3[] = "deliver";
    char str4[] = "star";
    char str5[] = "knits";

    cout << "The strings before reversing: " << endl;
    cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

    reverse(str1);
    reverse(str2);
    reverse(str3);
    reverse(str4);
    reverse(str5);

    cout << "The strings after reversing: " << endl;
    cout << str1 << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << endl;

    return 0;

}

Here is the reverse function:

#ifndef REVERSE_HPP
#define REVERSE_HPP
#include <string.h>

void reverse(char*temp)
{
    // *temp would equal "t" in the first cstring
    // (sizeof *temp - 1) would equal last character ???

}

#endif // REVERSE_HPP

Aucun commentaire:

Enregistrer un commentaire