Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in C only
Search
Advanced Search
Forums
BYTES
Product Launch
Updates
Developer Toolkit
Today's Posts
Member List
Calendar
Home
Forum
Topic
C
string reversal
Collapse
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
jerico
New Member
Join Date:
Sep 2006
Posts:
39
#1
string reversal
Oct 11 '06, 11:42 AM
Hi.How can a string be reversed without using a loop?thanks for any help.
Jerico
arne
Recognized Expert
Contributor
Join Date:
Oct 2006
Posts:
315
#2
Oct 11 '06, 12:15 PM
Originally posted by
jerico
Hi.How can a string be reversed without using a loop?thanks for any help.
Jerico
You mean something like
string string_reverse( const string& s ) {
string ret;
copy( s.rbegin(), s.rend(), back_inserter( ret ) );
return ret;
}
Comment
Post
Cancel
zahidkhan
New Member
Join Date:
Sep 2006
Posts:
22
#3
Oct 11 '06, 01:29 PM
Originally posted by
arne
You mean something like
string string_reverse( const string& s ) {
string ret;
copy( s.rbegin(), s.rend(), back_inserter( ret ) );
return ret;
}
If you want to avoid for loop or while loop then you have to use recursion
void str_reverse(cha r str[],int startIndex,int endIndex)
{
if(startIndex >= endIndex) return;
str_reverse(str ,++startIndex,--endIndex);
str[startIndex-1]^=str[endIndex]^=str[startIndex-1]^=str[endIndex];
}
int main(int argc,char* argv[])
{
char str[] = "MyString";
str_reverse(str ,0,strlen(str)) ;
printf("%s\n",s tr);
return 0;
}
Comment
Post
Cancel
D_C
Contributor
Join Date:
Jun 2006
Posts:
293
#4
Oct 11 '06, 07:58 PM
Just a hint, any tail end recursion can be converted into a while loop, and that's precisely what most (all?) compilers do.
Comment
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment