Outputting a string backwards?
Simple newbie question:
How do I get a string in a char array and then output it backwards? I tried a few things, but I can''t seem to get it to work right without outputting the extra parts of the array that gives me a result like
#*$#^@#$#$@esseJ
(with the name inputted as Jesse)
Anyone care to help a noob?
Shmiznac
Shmiznac
#include <iostream.h>
#include <windows.h>
int main()
{
char jesse[] = "Jesse";
cout << jesse << " backwards: ";
for(int i = (strlen((char *)jesse)-1); i > -1; i--)
{
cout << jesse; // without space, otherwise boardsthinks it's an italics code<br> }<br><br> cout << endl;<br><br> return 0;<br>}<br><br><a href="mailto:webwill666@hotmail.com">Will O'Connor</a>, <a href="http://www.angelfire.com/games4/flammaprod/" target="_blank">Flamma Productions</a>.<br><br><SPAN CLASS=editedby>[edited by - Will - Flamma Prod on March 13, 2002 7:58:32 PM]</SPAN>
#include <windows.h>
int main()
{
char jesse[] = "Jesse";
cout << jesse << " backwards: ";
for(int i = (strlen((char *)jesse)-1); i > -1; i--)
{
cout << jesse; // without space, otherwise boardsthinks it's an italics code<br> }<br><br> cout << endl;<br><br> return 0;<br>}<br><br><a href="mailto:webwill666@hotmail.com">Will O'Connor</a>, <a href="http://www.angelfire.com/games4/flammaprod/" target="_blank">Flamma Productions</a>.<br><br><SPAN CLASS=editedby>[edited by - Will - Flamma Prod on March 13, 2002 7:58:32 PM]</SPAN>
[email=webwill666@hotmail.com]Will O'Connor[/email], Flamma Productions.
C:
C++:
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
#include <stdio.h>#include <string.h>int main(){ char str[] = "This is a string"; strrev(str); printf("%s\n", str); return 0;}
C++:
#include <algorithm>#include <string>using namespace std;int main(){ string str = "This is a string"; reverse(str.begin(), str.end()); cout << str << endl; return 0;}
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Thanks for the help, I got it! My code was almost exactly like Will''s except I was using strlen incorrectly. Oluseyi those are some nifty functions that I haven''t learned yet. Argh, they make you do everything the hard way don''t they...
Shmiznac
Shmiznac
Shmiznac
Yeah, you feel pretty wasted when you find a function that does what you need - and is probably written in assembly so it''s faster than your version. Don''t worry, you''ll figure ''em out.
One thing you might want to do is look through header files and see what functions are in there. Also spend some time reading documentation and not programming.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
One thing you might want to do is look through header files and see what functions are in there. Also spend some time reading documentation and not programming.
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement