473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

string reversal

39
Hi.How can a string be reversed without using a loop?thanks for any help.

Jerico
Oct 11 '06 #1
3 2548
arne
315 Expert 100+
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;
}
Oct 11 '06 #2
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(char 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",str);
return 0;
}
Oct 11 '06 #3
D_C
293 100+
Just a hint, any tail end recursion can be converted into a while loop, and that's precisely what most (all?) compilers do.
Oct 11 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

74
by: Michael | last post by:
As if we needed another string reversal question. I have a problem with the following code, that I believe should work. int StringReverse(char* psz) { char *p = psz; char *q = psz +...
45
by: Rakesh | last post by:
Hi, I have this function to reverse the given string. I am just curious if that is correct and there could be better way of doing it / probable bugs in the same. The function prototype is...
24
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include...
6
by: ashok.anbalan | last post by:
I tried using the string reversal routine that occurs in K & R 2nd edition (Pg. 62) & the program core dumps. #include <stdio.h> #include <stdlib.h> void reverse ( char s ) { int c, i, j;
12
by: Sathyaish | last post by:
How would you reverse a string "in place" in python? I am seeing that there are a lot of operations around higher level data structures and less emphasis on primitive data. I am a little lost and...
25
by: Frederick Gotham | last post by:
I was intrigued by someone the other day who posted regarding methods of "mirror-imaging" the bits in a byte. I thought it might be interesting to write a fully-portable algorithm for...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
3
by: steezli | last post by:
Hi, Brand new to VB.NET and I'm having a problem figuring out this program. I'll try and be descritive as possible. I have to create a Windows application that contains a single top-level...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
144
by: dominantubergeek | last post by:
Hello, I'm a highly experienced expert C programmer and I've written this code to reverse a string in place. I think you could all learn something from it! int reverse(char* reverseme){ int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.