User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,369 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,047 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1620 | Replies: 6 | Solved
Reply
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

C++ Array Question

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ Array Question

  #2  
Oct 27th, 2006
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--]);
Reply With Quote  
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

Troubleshooting Re: C++ Array Question

  #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
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ Array Question

  #4  
Oct 27th, 2006
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.
Reply With Quote  
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

Re: C++ Array Question

  #5  
Oct 27th, 2006
Originally Posted by Ancient Dragon View Post
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.
Last edited by Juggler : Oct 27th, 2006 at 10:55 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 878
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: C++ Array Question

  #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  
Join Date: Oct 2006
Posts: 8
Reputation: Juggler is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Juggler Juggler is offline Offline
Newbie Poster

Re: C++ Array Question

  #7  
Oct 27th, 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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC