Monday, May 24, 2010

In C++, how do you write a program that reverses a string?

go to help you will see many examples on that

In C++, how do you write a program that reverses a string?
Why do you keep asking the same question? Twice today? For a routine common in learner texts and help files everywhere?





A general answer: check out open source software code. The Linux utilities code that routine in C and I suppose a C++ variant is rattling around somewhere.





All Linux distributions provide source code for download. Stumble around http://distrowatch.com and discover a convenient source for your source code!





I suppose you realize that Redmond's (Bill Gates') C++ variant is purposely different than the ISO approved C++, but that is another issue.
Reply:The reason you're not getting any real helpful responses is because this sounds like a homework question. I doubt the teacher wants you to use a library function, but for you to create it yourself. Read about char arrays. Should figure it out from there.
Reply:strrev() can be used to reverse a string. You can find more details abt the function in the documentation.
Reply:Reversing the string using the Triple XOR Process





int StringReverse(char* strToReverse)


{


if (NULL==strToReverse)


return -1;





int length=strlen(strToReverse)-1;


if (1==length)


return 1;





for(int index=0; index%26lt;length; index++,length--)


{


strToReverse[index] ^= strToReverse[length];


strToReverse[length] ^= strToReverse[index];


strToReverse[index] ^= strToReverse[length];


}


return 0;


}


No comments:

Post a Comment