1

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

This is different than others that were posted because I'm trying to get the length with a pointer

nbarbop24
  • 13
  • 4
  • 3
    try some things, print the results so you see what is happening. you can increment a pointer. try that and see what it points to by printing out the contents. experiment. – john elemans Dec 01 '16 at 19:02
  • 1
    thats what strlen is for. https://linux.die.net/man/3/strlen – pm100 Dec 01 '16 at 19:02
  • `char str1[] = "time";` is equivalent to `char str1[5] = {'t', 'i', 'm', 'e', '\0'};` – Jarod42 Dec 01 '16 at 19:38

3 Answers3

2

just for completeness

void reverse(char*temp)
{
    int l = strlen(temp);
  .....
}

For extra credit, re-implement the whole assignment (main as well) using c++ strings, which is what you should use in c++. Also put the strings in a vector rather than n separate variables.

Sigh - I see somebody else already did the first one for you.You could still make the reverse function work on the std::string rather than c_str(). Note that c++ has a oneliner for you that will reverse the string - find that for extra extra credit

pm100
  • 48,078
  • 23
  • 82
  • 145
1

Finding the length of an array through a pointer is not possible. But your arrays were initialized from string literals, strings in c and c++ have a special way to be stored that allows you to determine it's length.

A string is defined as a sequence of non-null characters followed by a null character, so to find the length of a string you simply have to iterate through the characters until you find the '\0', like this

size_t length = 0;
while (str[length] != '\0')
    ++length;

you can of course, use the strlen() function provided by the c standard library and available in the string.h header.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • this is really an XY problem, he needs the length of a string, not a general purpose way of finding an array length – pm100 Dec 01 '16 at 19:04
  • I changed the question title - to make it completely clear what OP is really asking – pm100 Dec 01 '16 at 19:08
  • The point is to assign pointers to the front and rear of the string and use the reverse function for all of the cstrings. Using strlen would work if I didn't need the reverse function to work for all of the cstrings – nbarbop24 Dec 01 '16 at 19:10
  • @nbarbop24 I'm not sure to understand how you are implementing your reverse function. Knowing the length of the c-string you can properly initialize the pointer to the end. – Bob__ Dec 01 '16 at 19:24
  • I'm passing the cstring to the reverse function and I'm assigning the cstring to a pointer. I need to find the length of the cstring as it is being passed by the pointer. So for example, strlen(*temp) will not work – nbarbop24 Dec 01 '16 at 19:30
  • @nbarbop24 Ok, I think you should read this answer again... – Bob__ Dec 01 '16 at 19:38
0

In c++, you want to use std::string

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

using namespace std;

// why the declaration here?
// (I would presume this goes in "reverse.hpp" ?)
void reverse(std::string& str);

int main()
{
  string str1 = "time";
  string str2 = "straw";
  string str3 = "deliver";
  string str4 = "star";
  string 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;
}

Do not forget to change

reverse(const char*);

to reverse( std::string& );

std::string is real easy to use, and unlike pointers, it carries all you need.

Bamaco
  • 592
  • 9
  • 25
  • Then it should be `std::string reverse( const std::string &str )` – Slava Dec 01 '16 at 19:13
  • @Slava, I agree, I just proposed void reverse( std::string& str ); to more closely match the question, but I would also do this with the prototype you used. – Bamaco Dec 01 '16 at 19:19