C++ Array Question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

C++ Array Question

 
0
  #1
Oct 27th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Array Question

 
0
  #2
Oct 27th, 2006
please post code so we don't have to guess what you are doing. But basically its like this
  1. char string[] = "Hello World";
  2. int len = strlen(string)-1;
  3. while(len >= 0)
  4. printf("%c",string[len--]);
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

Re: C++ Array Question

 
0
  #3
Oct 27th, 2006
Originally Posted by Ancient Dragon View Post
please post code so we don't have to guess what you are doing. But basically its like this
  1. char string[] = "Hello World";
  2. int len = strlen(string)-1;
  3. while(len >= 0)
  4. 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.

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

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Array Question

 
0
  #4
Oct 27th, 2006
strcpy() function does not take 3 parameters --
  1. 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)
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

Re: C++ Array Question

 
0
  #5
Oct 27th, 2006
Originally Posted by Ancient Dragon View Post
strcpy() function does not take 3 parameters --
  1. 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)
  1. 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.

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

Thank you. Sorry for the questions, but I'm not quite understanding arrays totally yet.
Last edited by Juggler; Oct 27th, 2006 at 11:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,502
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1479
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ Array Question

 
0
  #6
Oct 27th, 2006
here is how I would code it.
cout << "The string reversed: ";
for (i = strlen(reversedUserInput)-1; i >= 0; i--)
{
// print the ith character
    cout << reversedUserInput[i]; 
}
cout << "\n";
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

Re: C++ Array Question

 
0
  #7
Oct 28th, 2006
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC