943,964 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 17848
  • C++ RSS
Dec 28th, 2004
0

Reversing a string?

Expand Post »
Hello ladies and gents,

I'm trying to reverse a string when one is entered with cin.getline, so when typing for example: testing

My output should become: gnitset

Ive written this piece of code and it works but, because the string has a certain length and not the whole length of the string is neccesarly being used the part wich is not used is also being shown in my output?

How do I get rid of this?

C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. char tekst[20];
  4.  
  5. cout<< "Put in a line of text! ";
  6.  
  7. cin.getline(tekst, 20);
  8.  
  9. cout<< "Reverse the string now!" <<endl;
  10.  
  11. for (int i=0; i<sizeof tekst; i++)
  12. cout<< tekst [sizeof tekst-i-1];
  13.  
  14. cout<<endl;
  15. return 0;
  16. }

Allready tried a search on this topic, came up with four possibilities and none really was sufficient for my problem, so thanks for the help in advance
Similar Threads
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Dec 28th, 2004
0

Re: Reversing a string?

I copied this off of www.csharphelp.com

C++ Syntax (Toggle Plain Text)
  1. int Reverse(char* str)
  2. {
  3.  
  4. if (NULL==str)return -1; //no string
  5. int l=strlen(str)-1; //get the string length
  6. if (1==l)return 1;
  7.  
  8. for(int x=0;x < l;x++,l--)
  9. {
  10. str[x]^=str[l]; //triple XOR Trick
  11. str[l]^=str[x]; //for not using a temp
  12. str[x]^=str[l];
  13. }
  14.  
  15. return 0;
  16.  
  17. }

Also, check out the following:

http://www.daniweb.com/code/snippet117.html
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Dec 28th, 2004
0

Re: Reversing a string?

Don't use the physical size of the string as your limit, use the logical size. The physical size is the size of the array while the logical size is determined by where the terminating null character is:
C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2. #include <cstring>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. void sreverse(char *s)
  8. {
  9. size_t begin = 0;
  10. size_t end = strlen(s) - 1;
  11.  
  12. while (begin < end)
  13. swap(s[begin++], s[end--]);
  14. }
  15.  
  16. int main()
  17. {
  18. char s[20];
  19.  
  20. cout<<"Enter a string: ";
  21. if (cin.getline(s, sizeof s)) {
  22. sreverse(s);
  23. cout<< s <<endl;
  24. }
  25. }
Or alternatively, straight printing as you were doing:
C++ Syntax (Toggle Plain Text)
  1. #include <cstring>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char s[20];
  9.  
  10. cout<<"Enter a string: ";
  11. if (cin.getline(s, sizeof s)) {
  12. for (size_t i = strlen(s) - 1; i != 0; i--)
  13. cout<< s[i];
  14. cout<< s[0] <<endl;
  15. }
  16. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 28th, 2004
0

Re: Reversing a string?

Narue, I see in your example you use #include <algorithm>
I never heard of this library? I did a google search for algorithm.h and can't seem to find a listing of functions?
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Dec 28th, 2004
0

Re: Reversing a string?

>I never heard of this library?
It's a handy header. Among the functions that it gives you is swap, which saves everyone from that XOR abomination that your code displayed. *shudder* At least it wasn't undefined behavior the way you wrote it. Most people try to palm off the one-liner version:
C++ Syntax (Toggle Plain Text)
  1. a ^= b ^= a ^= b;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 28th, 2004
0

Re: Reversing a string?

Thank you very much ladies

Narue, ever tought of becoming a teacher, I'm certain you'd be great at it
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Dec 28th, 2004
0

Re: Reversing a string?

I was actually thinking that it probably did cover the swap function() which is why I did a google search for it. When I couldn't come up with a list of functions it included, I figured I'd just ask you directly. I'm going to recede back into my PHP world just about now
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Dec 28th, 2004
0

Re: Reversing a string?

>Narue, ever tought of becoming a teacher
Yes, but if it's anything like the "tutoring" I do with the programmers working under me, I would be fired in an instant for verbal abuse. It's not that I'm really that bad, it's just that people are too touchy these days.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 28th, 2004
0

Re: Reversing a string?

ROFLMAO, oh those employees are so touchy these days :mrgreen:

Before I forget, thanks for the links Dani
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

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: Problems with character strings
Next Thread in C++ Forum Timeline: The Eternal Sun - open-source MMORPG needs coders





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


Follow us on Twitter


© 2011 DaniWeb® LLC