I have a question about manipulating an array in C++. In my program, a cstring is entered. The string is printed. Finally, the string must be printed in reverse and displayed.

The final part is where I am having my problem. I have the strlen function determine the legnth of the function, and I was then going manipulate the string by having the array go to the last character of the string entered, and then print each of the characters by cycling through the c-string backwards.

I am having trouble with this. I can't seem to figure out how to get the strlen function to be the starting point of the array. Any suggestions?

Thank you.

Recommended Answers

All 6 Replies

please post code so we don't have to guess what you are doing. But basically its like this

char string[] = "Hello World";
int len = strlen(string)-1;
while(len >= 0)
  printf("%c",string[len--]);

please post code so we don't have to guess what you are doing. But basically its like this

char string[] = "Hello World";
int len = strlen(string)-1;
while(len >= 0)
  printf("%c",string[len--]);

Is this close to working? I get an error at the first line of code posted. I've checked and there are no typos at reversedUserInput or userInput.

strcpy(reversedUserInput, userInput, strlen(userInput));
for (reversedUserInput; i >= 0; i--)
{
  cout << "The string reversed:\n" << reversedUserInput << endl;                
}

Thanks.

strcpy() function does not take 3 parameters --

strcpy(reversedUserInput, userInput);

>>for (reversedUserInput; i >= 0; i--)
This is incorrect. you have to initialize variable i with the length of reversedUserInput string minus 1 (because of null terminator)

for (i = strlen(reversedUserInput)-1; i >= 0; i--)

>>cout << "The string reversed:\n" << reversedUserInput << endl
This is incorrect too. It will display the entire string in normal order (not reverse order). It should print just one character on each loop iteration.

strcpy() function does not take 3 parameters --

strcpy(reversedUserInput, userInput);

>>for (reversedUserInput; i >= 0; i--)
This is incorrect. you have to initialize variable i with the length of reversedUserInput string minus 1 (because of null terminator)

for (i = strlen(reversedUserInput)-1; i >= 0; i--)

>>cout << "The string reversed:\n" << reversedUserInput << endl
This is incorrect too. It will display the entire string in normal order (not reverse order). It should print just one character on each loop iteration.

I actually caught the strcpy/strncpy mistake soon after I posted this.

If you don't mind, can you see if the attached code looks better?

I am still confused about the the last part of code that you corrected where the cout is. I'm not exactly sure how to go about getting this to go one character at a time. Would something like reversedUserInput i++ be used? I see what you mean though. I get the string printed the amount of times that there are characters in the string.

strncpy(reversedUserInput, userInput, strlen(userInput));
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
{
cout << "The string reversed:\n" << reversedUserInput << endl; 
}

Thank you. Sorry for the questions, but I'm not quite understanding arrays totally yet.

here is how I would code it.

cout << "The string reversed: ";
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
{
// print the [b]ith[/b] character
    cout << reversedUserInput[i]; 
}
cout << "\n";

Thank you for your help. I didn't use your code exactly, but it helped me figure out what I was doing wrong with mine. Amazing how obvious stuff sometimes causes so much trouble. I spent a good 45 minutes trying to figure out why cout was printing each letter in it's own line. INSIDE and OUTSIDE of a loop seems to make a BIG difference!!!

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.