Efficient way to reverse the string inplace in c++??
-
Expecting favourable reply.
-
Expecting favourable reply.
Super Hornet wrote:
Expecting favourable reply
Explecting explicit and clear question, with code snippets and all that i need to understand the point with ambiguity...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Super Hornet wrote:
Expecting favourable reply
Explecting explicit and clear question, with code snippets and all that i need to understand the point with ambiguity...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
//function that retuens the reverse of passed string
char *ReverseString(char *pszString)
{
if (pszString) //check for valid pointer
{
int ilength = strlen(pszString) + 1; //length of string including '\0'for (int iIndex = ilength; iIndex > 0; iIndex--) { pszString\[i\] = pszString\[i-1\]; } pszString\[0\] = '\\0'; return &pszString\[ilength\]; } else { return NULL; }
}
//program to reverse the string inplace
int main()
{
char szString[] = "hello world";//prints the string before calling ReverseString method cout << "String before reversing " << szString; szString = ReverseString(szStirng); //returns the reverse of a string //prints the string after calling ReverseString method cout << "\\n String after reversing " << szString;
}
Is the above code correct???
-
//function that retuens the reverse of passed string
char *ReverseString(char *pszString)
{
if (pszString) //check for valid pointer
{
int ilength = strlen(pszString) + 1; //length of string including '\0'for (int iIndex = ilength; iIndex > 0; iIndex--) { pszString\[i\] = pszString\[i-1\]; } pszString\[0\] = '\\0'; return &pszString\[ilength\]; } else { return NULL; }
}
//program to reverse the string inplace
int main()
{
char szString[] = "hello world";//prints the string before calling ReverseString method cout << "String before reversing " << szString; szString = ReverseString(szStirng); //returns the reverse of a string //prints the string after calling ReverseString method cout << "\\n String after reversing " << szString;
}
Is the above code correct???
hum... bad habits lead to bad code... you're using C++, so use it entierely. first of all, abandon the C-style strings (char*), it's just a pain to use when you can make use of the string class. here is another solution:
std::string ReverseString(const std::string& str) {
std::string strTmp;std::string::const\_reverse\_iterator iter; for (iter = str.rbegin(); iter != rend(); --iter) { strTmp += \*iter; } return strTmp;
}
you could also use the swap technic:
std::string ReverseString(const std::string& str) {
std::string strTmp = str;size\_t len = strTmp.length(); for (size\_t i = 0; iter < len/2; i++) { strTmp\[i\] = str\[len-i\]; strTmp\[len-i\] = str\[i\]; } return strTmp;
}
or even more powerful, using the STL algorithms (thanks jijo raj):
std::string ReverseString(const std::string& str) {
return std::reverse(str.begin(), str.end());
}now in your main, just call it:
int main(int argc, char* argv[]) {
std::string s = "Hello World";std::cout << "Before reversing : " << s <<endl; std::cout << "After reversing : " << ReverseString(s) << endl;
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Wednesday, August 6, 2008 6:00 AM
-
hum... bad habits lead to bad code... you're using C++, so use it entierely. first of all, abandon the C-style strings (char*), it's just a pain to use when you can make use of the string class. here is another solution:
std::string ReverseString(const std::string& str) {
std::string strTmp;std::string::const\_reverse\_iterator iter; for (iter = str.rbegin(); iter != rend(); --iter) { strTmp += \*iter; } return strTmp;
}
you could also use the swap technic:
std::string ReverseString(const std::string& str) {
std::string strTmp = str;size\_t len = strTmp.length(); for (size\_t i = 0; iter < len/2; i++) { strTmp\[i\] = str\[len-i\]; strTmp\[len-i\] = str\[i\]; } return strTmp;
}
or even more powerful, using the STL algorithms (thanks jijo raj):
std::string ReverseString(const std::string& str) {
return std::reverse(str.begin(), str.end());
}now in your main, just call it:
int main(int argc, char* argv[]) {
std::string s = "Hello World";std::cout << "Before reversing : " << s <<endl; std::cout << "After reversing : " << ReverseString(s) << endl;
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Wednesday, August 6, 2008 6:00 AM
You can further optimize it by using
std::reverse()
algorithm...// Reverse it by using the Reverse alogrithm.
string Str = _T("Hello");
reverse( Str.begin(), Str.end());Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
hum... bad habits lead to bad code... you're using C++, so use it entierely. first of all, abandon the C-style strings (char*), it's just a pain to use when you can make use of the string class. here is another solution:
std::string ReverseString(const std::string& str) {
std::string strTmp;std::string::const\_reverse\_iterator iter; for (iter = str.rbegin(); iter != rend(); --iter) { strTmp += \*iter; } return strTmp;
}
you could also use the swap technic:
std::string ReverseString(const std::string& str) {
std::string strTmp = str;size\_t len = strTmp.length(); for (size\_t i = 0; iter < len/2; i++) { strTmp\[i\] = str\[len-i\]; strTmp\[len-i\] = str\[i\]; } return strTmp;
}
or even more powerful, using the STL algorithms (thanks jijo raj):
std::string ReverseString(const std::string& str) {
return std::reverse(str.begin(), str.end());
}now in your main, just call it:
int main(int argc, char* argv[]) {
std::string s = "Hello World";std::cout << "Before reversing : " << s <<endl; std::cout << "After reversing : " << ReverseString(s) << endl;
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Wednesday, August 6, 2008 6:00 AM
Dosen't string have a
reserve()
function? :confused:nave [OpenedFileFinder]
-
Dosen't string have a
reserve()
function? :confused:nave [OpenedFileFinder]
Naveen wrote:
Dosen't string have a reserve() function?
reserve() ? what for ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
You can further optimize it by using
std::reverse()
algorithm...// Reverse it by using the Reverse alogrithm.
string Str = _T("Hello");
reverse( Str.begin(), Str.end());Regards, Jijo.
_____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
damn it, i was sure such a functor was existing somewhere, but i couldn't put a hand on it though. thanks for the notice.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Naveen wrote:
Dosen't string have a reserve() function?
reserve() ? what for ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
ops My bad. sorry I miss read the reserve as reverse.
nave [OpenedFileFinder]
-
damn it, i was sure such a functor was existing somewhere, but i couldn't put a hand on it though. thanks for the notice.
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
hum... bad habits lead to bad code... you're using C++, so use it entierely. first of all, abandon the C-style strings (char*), it's just a pain to use when you can make use of the string class. here is another solution:
std::string ReverseString(const std::string& str) {
std::string strTmp;std::string::const\_reverse\_iterator iter; for (iter = str.rbegin(); iter != rend(); --iter) { strTmp += \*iter; } return strTmp;
}
you could also use the swap technic:
std::string ReverseString(const std::string& str) {
std::string strTmp = str;size\_t len = strTmp.length(); for (size\_t i = 0; iter < len/2; i++) { strTmp\[i\] = str\[len-i\]; strTmp\[len-i\] = str\[i\]; } return strTmp;
}
or even more powerful, using the STL algorithms (thanks jijo raj):
std::string ReverseString(const std::string& str) {
return std::reverse(str.begin(), str.end());
}now in your main, just call it:
int main(int argc, char* argv[]) {
std::string s = "Hello World";std::cout << "Before reversing : " << s <<endl; std::cout << "After reversing : " << ReverseString(s) << endl;
}
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
modified on Wednesday, August 6, 2008 6:00 AM
Thanks a-lot toxcct
-
//function that retuens the reverse of passed string
char *ReverseString(char *pszString)
{
if (pszString) //check for valid pointer
{
int ilength = strlen(pszString) + 1; //length of string including '\0'for (int iIndex = ilength; iIndex > 0; iIndex--) { pszString\[i\] = pszString\[i-1\]; } pszString\[0\] = '\\0'; return &pszString\[ilength\]; } else { return NULL; }
}
//program to reverse the string inplace
int main()
{
char szString[] = "hello world";//prints the string before calling ReverseString method cout << "String before reversing " << szString; szString = ReverseString(szStirng); //returns the reverse of a string //prints the string after calling ReverseString method cout << "\\n String after reversing " << szString;
}
Is the above code correct???
Super Hornet wrote:
Is the above code correct???
That all depends on what you were trying to accomplish. The use of
cout
appears to be the only thing C++ that I see."Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch