![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: reverse, string |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| Seems like their is some circular logic going on here... incrementing while decrementing - or is String a reserved word? or are you supposed to reference String as a pointer in the FUnction too? (like *String instead of String)? Here's a recursive fuinction reversiing a string in C++ Code: Recursive Reverse String Algorithm
The Shortest function to reverse a string.
Written by: Sanchit Karve
born2c0de AT hotmail.com
[born2c0de]
*/
#include <iostream>
using namespace std;
void ret_str(char* s)
{
if(*s != '\0')
ret_str(s+1);
cout<<*(s);
}
int main()
{
ret_str("born2c0de");
return 0;
} __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| The Following User Says Thank You to ccoonen For This Useful Post: | ||
HelloWorld (03-28-2008) | ||
| ||||
| You could use a for loop of the string's length and do something like Code: ReversedString[i] = String[StringLength-i]; |
| The Following User Says Thank You to MrPickle For This Useful Post: | ||
HelloWorld (03-30-2008) | ||
| |||
| the problem is that "hello" is a string literal, and cannot be modified, so its undefined behaviour and VS is right to throw up an error. change your main to something like this Code: int main()
{
//"hello" is now copied into a char[]
char str[] = "hello";
ReverseString( str );
} edit you're also missing the code which actually swaps the characters in your string - all you're doing is iterating 2 pointers which don't actually modify the char array Last edited by Lee : 03-30-2008 at 05:42 PM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (03-30-2008) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |