| | |
Reversing a string?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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?
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
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)
int main() { char tekst[20]; cout<< "Put in a line of text! "; cin.getline(tekst, 20); cout<< "Reverse the string now!" <<endl; for (int i=0; i<sizeof tekst; i++) cout<< tekst [sizeof tekst-i-1]; cout<<endl; return 0; }
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
I copied this off of www.csharphelp.com
Also, check out the following:
http://www.daniweb.com/code/snippet117.html
C++ Syntax (Toggle Plain Text)
int Reverse(char* str) { if (NULL==str)return -1; //no string int l=strlen(str)-1; //get the string length if (1==l)return 1; for(int x=0;x < l;x++,l--) { str[x]^=str[l]; //triple XOR Trick str[l]^=str[x]; //for not using a temp str[x]^=str[l]; } return 0; }
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
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:
Or alternatively, straight printing as you were doing:
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <cstring> #include <iostream> using namespace std; void sreverse(char *s) { size_t begin = 0; size_t end = strlen(s) - 1; while (begin < end) swap(s[begin++], s[end--]); } int main() { char s[20]; cout<<"Enter a string: "; if (cin.getline(s, sizeof s)) { sreverse(s); cout<< s <<endl; } }
C++ Syntax (Toggle Plain Text)
#include <cstring> #include <iostream> using namespace std; int main() { char s[20]; cout<<"Enter a string: "; if (cin.getline(s, sizeof s)) { for (size_t i = strlen(s) - 1; i != 0; i--) cout<< s[i]; cout<< s[0] <<endl; } }
New members chased away this month: 4
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?
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
>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:
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)
a ^= b ^= a ^= b;
New members chased away this month: 4
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

Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
>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.
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
![]() |
Similar Threads
- reversing a string (C++)
- Reversing A String (C)
- Reversing a String which is inputed. (Java)
- Reversing a string using recursion (Java)
- Reversing a string (C++)
- Code Snippet: reversing a string (C++)
Other Threads in the C++ Forum
- Previous Thread: Problems with character strings
- Next Thread: Programming Ideas
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






