How do you reverse string elements

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

Join Date: Nov 2006
Posts: 6
Reputation: hui is an unknown quantity at this point 
Solved Threads: 0
hui hui is offline Offline
Newbie Poster

How do you reverse string elements

 
0
  #1
Nov 1st, 2006
//My function is but the problem is that i dont have a proper working main function
void reverse(int a[], int n) { // function: Reverses the elements of a. // parameters: // a inout array of values. // n in number of values in a, a[0]..a[n-1] // returns: nothing for (int i=0; i<n/2; i++) { int temp = a[i]; a[i] = a[n-i-1]; a[n-i-1] = temp; } return;
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How do you reverse string elements

 
0
  #2
Nov 1st, 2006
Sorry, but you can't really expect that someone will understand your question or function if you post like this.
Retry and please use CODE-tags, indent your code, post the complete code and ask a clear question.

gr Niek
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: hui is an unknown quantity at this point 
Solved Threads: 0
hui hui is offline Offline
Newbie Poster

Please help with code for reversing string elements

 
0
  #3
Nov 1st, 2006
/*My function is but the problem is that i dont have a proper working main function*/

  1. void reverse(int a[], int n)
  2. {
  3.  
  4. for (int i=0; i<n/2; i++) {
  5.  
  6. int temp = a[i];
  7.  
  8. a[i] = a[n-i-1];
  9.  
  10. a[n-i-1] = temp;
  11.  
  12. }
  13.  
  14. }
Last edited by WolfPack; Nov 1st, 2006 at 6:05 am. Reason: Added [CODE][/CODE] tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How do you reverse string elements

 
0
  #4
Nov 1st, 2006
the problem is that i dont have a proper working main function
I see...but there are more problems. I'm guessing that you want to reverse a string?? Have a look at this , last post. It's in dutch but I think you will find the code helpfull.

gr Niek
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: How do you reverse string elements

 
0
  #5
Nov 1st, 2006
Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void ReverseString(char string1[], char reversed[]);
  6.  
  7. int main()
  8. {
  9. const int MAX = 80;
  10. char string1[MAX];
  11. char reversed[MAX];
  12. cout << "Enter your string: ";
  13. cin.get(string1,MAX);
  14.  
  15. ReverseString(string1, reversed);
  16.  
  17. return 0;
  18. }
  19.  
  20. void ReverseString(char string1[], char reversed[])
  21. {
  22. int length = strlen(string1);
  23.  
  24. for (int i=length-1, j=0; i>=0; i--, j++)
  25. reversed[j] = string1[i];
  26.  
  27. for (i=0; i<length; i++)
  28. cout << reversed[i];
  29.  
  30. cout << endl;
  31. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How do you reverse string elements

 
0
  #6
Nov 1st, 2006
Originally Posted by may4life View Post
Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void ReverseString(char string1[], char reversed[]);
  6.  
  7. int main()
  8. {
  9. const int MAX = 80;
  10. char string1[MAX];
  11. char reversed[MAX];
  12. cout << "Enter your string: ";
  13. cin.get(string1,MAX);
  14.  
  15. ReverseString(string1, reversed);
  16.  
  17. return 0;
  18. }
  19.  
  20. void ReverseString(char string1[], char reversed[])
  21. {
  22. int length = strlen(string1);
  23.  
  24. for (int i=length-1, j=0; i>=0; i--, j++)
  25. reversed[j] = string1[i];
  26.  
  27. for (i=0; i<length; i++)
  28. cout << reversed[i];
  29.  
  30. cout << endl;
  31. }
You really should compile and test your code before posting. It contains several bugs that may just confuse the OP.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: How do you reverse string elements

 
0
  #7
Nov 1st, 2006
  1.  
  2. void ReverseString(char string1[], char reversed[])
  3. {
  4. int i, length = strlen(string1);
  5. for (int i=length-1, j=0; i>=0; i--, j++)
  6. reversed[j] = string1[i];
  7. for (i=0; i<length; i++)
  8. cout << reversed[i];
  9. cout << endl;
  10. cin.get();
  11. cin.get();
  12. }

Here's the reviewed code from may4life.

gr Niek
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: How do you reverse string elements

 
0
  #8
Nov 1st, 2006
Hmm, dont know whats going wrong.. I copied and pasted my code from here and still works no prob on my compiler :-S
I always test my code and post it with 0 errors and 0 warnings.. Maybe there might be a mix up with our compilers.. Sorry for anyone who had problems with my code..but they work for me :-S
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