Reversing a string?

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

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Reversing a string?

 
0
  #1
Dec 28th, 2004
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?

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,056
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Reversing a string?

 
0
  #2
Dec 28th, 2004
I copied this off of www.csharphelp.com

  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
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Reversing a string?

 
0
  #3
Dec 28th, 2004
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:
  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:
  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. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,056
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Reversing a string?

 
0
  #4
Dec 28th, 2004
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?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Reversing a string?

 
0
  #5
Dec 28th, 2004
>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:
  1. a ^= b ^= a ^= b;
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Reversing a string?

 
0
  #6
Dec 28th, 2004
Thank you very much ladies

Narue, ever tought of becoming a teacher, I'm certain you'd be great at it
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,056
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Reversing a string?

 
0
  #7
Dec 28th, 2004
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
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Reversing a string?

 
0
  #8
Dec 28th, 2004
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Reversing a string?

 
0
  #9
Dec 28th, 2004
ROFLMAO, oh those employees are so touchy these days :mrgreen:

Before I forget, thanks for the links Dani
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC