Tuesday, July 28, 2009

Reverse a string in c++?

i wrote this code to reverse a string


string s1;


string s2;


int u=s1.length();


int i=u-1;


for(int j=0;j%26lt;u;j++)


{


s2[j]=s1[i];


i--;


}


but i have errors in run time , whats wrong?

Reverse a string in c++?
Your destination string starts off empty and you are trying to set a value in somewhere that doesn't exist. Also you don't have any initial value for s1.





So give s1 a value


string s1="amanaplanacanalpanama";


and set s2 to the same length


s2=s1;


Now it will work.
Reply:wtfwhat do u mean what is rong with it it is all mumbo jumbo


it would help if you actually explained it in english then go from there








haha no.

imperial

No comments:

Post a Comment