944,058 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4512
  • C++ RSS
Oct 27th, 2006
0

C++ Array Question

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juggler is offline Offline
8 posts
since Oct 2006
Oct 27th, 2006
0

Re: C++ Array Question

please post code so we don't have to guess what you are doing. But basically its like this
C++ Syntax (Toggle Plain Text)
  1. char string[] = "Hello World";
  2. int len = strlen(string)-1;
  3. while(len >= 0)
  4. printf("%c",string[len--]);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 27th, 2006
0

Re: C++ Array Question

please post code so we don't have to guess what you are doing. But basically its like this
C++ Syntax (Toggle Plain Text)
  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.

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

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juggler is offline Offline
8 posts
since Oct 2006
Oct 27th, 2006
0

Re: C++ Array Question

strcpy() function does not take 3 parameters --
C++ Syntax (Toggle Plain Text)
  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)
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 27th, 2006
0

Re: C++ Array Question

strcpy() function does not take 3 parameters --
C++ Syntax (Toggle Plain Text)
  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)
C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juggler is offline Offline
8 posts
since Oct 2006
Oct 27th, 2006
0

Re: C++ Array Question

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";
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,954 posts
since Aug 2005
Oct 28th, 2006
0

Re: C++ Array Question

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Juggler is offline Offline
8 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error: Cannot convert double ()() to double
Next Thread in C++ Forum Timeline: "conflicting types" error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC