Sunday, July 26, 2009

How to get reverse of a string in C/C++?{Try to tell in ANSI GCC, but TC command would also do}?

as if


hello =%26gt; olleh


bye =%26gt; eyb


etc...

How to get reverse of a string in C/C++?{Try to tell in ANSI GCC, but TC command would also do}?
Here's one in C++








#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





int main()


{


string userInput;


cout %26lt;%26lt; "Please enter your word: ";


cin %26gt;%26gt; userInput;





string str;





for (int i = 0; i %26lt; userInput.size(); ++i)


{


str[i] = userInput[(userInput.size()) - i)];


}





cout %26lt;%26lt; "The word is now: " %26lt;%26lt; str;








system("PAUSE");


return 0;


}
Reply:Use STRREV function: http://www.itlnet.net/programming/progra...
Reply:try this function,





// you have to pass the String as a character array,





void reverseString(char * str){


int i = 0;


char tmp;


for( ; (str+i); i++) {


tmp = *(str + strlen(str) - i - 1);


*(str + strlen(str) - i - 1) = *(str + i) ;


*(str + i) = tmp;


}


}
Reply:http://www.dreamincode.net/code/snippet3...
Reply:Try the XOR operator, it's the easiest way to do it.
Reply:i m telling you the code in Turbo C++ compiler





char *a;


int i=0, count=0;


gets (a); // scan the string whose reverse is to be printed





while(a[i])


{


i++; count++;


} // this code will give you the lenght of this code.





for(i=0; i%26lt;count;i++)


{


printf("%c", a[count-i]);


} // this code will print the reverse of the string.








hope you got the solution...
Reply:For C++





string toBeRev; //some string you want to reverse





string rev = "";


for(unsigned int i = toBeRev.size(); i %26gt; 0; i--)


rev.push_back(toBeRev.at(i));





//rev will be the reversed string


No comments:

Post a Comment