944,087 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7776
  • C++ RSS
Nov 1st, 2006
0

How do you reverse string elements

Expand Post »
//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;
Similar Threads
hui
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hui is offline Offline
6 posts
since Nov 2006
Nov 1st, 2006
0

Re: How do you reverse string elements

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.
Last edited by Nick Evan; Mar 26th, 2010 at 9:36 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 1st, 2006
0

Please help with code for reversing string elements

/*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
hui
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hui is offline Offline
6 posts
since Nov 2006
Nov 1st, 2006
0

Re: How do you reverse string elements

Quote ...
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.
Last edited by Nick Evan; Mar 26th, 2010 at 9:36 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 1st, 2006
0

Re: How do you reverse string elements

Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 posts
since Oct 2006
Nov 1st, 2006
0

Re: How do you reverse string elements

Click to Expand / Collapse  Quote originally posted by may4life ...
Here's a working example of how to reverse a string using a function. Hope this helps, good luck!

C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Nov 1st, 2006
0

Re: How do you reverse string elements

C++ Syntax (Toggle Plain Text)
  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.
Last edited by Nick Evan; Mar 26th, 2010 at 9:36 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 1st, 2006
0

Re: How do you reverse string elements

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
Reputation Points: 13
Solved Threads: 2
Junior Poster in Training
may4life is offline Offline
57 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: Need help creating a simple Counter program using classes
Next Thread in C++ Forum Timeline: Simple Addition Question





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


Follow us on Twitter


© 2011 DaniWeb® LLC