| | |
C-String reverse
Please support our C++ advertiser: Intel Parallel Studio Home
A small function which reverses a c-string by just swapping the elements
...
Usage::
...Usage::
C++ Syntax (Toggle Plain Text)
char[] str = "hello"; reverse(str); // str now contains "olleh"
/********************************** * Written by Mathias Van Malderen * **********************************/ void reverse(char p[]) { int len=strlen(p); char t; for(int i=(--len), j=0; i>len/2; i--, j++) { // exchange elements t=p[i]; p[i]=p[j]; p[j]=t; } }
0
•
•
•
•
For C style strings there is <string.h>'s _strrev() and for C++'s strings there's <algorithm>'s reverse().
_strrev() is generally provided in most compilers.
_strrev() is generally provided in most compilers.
0
•
•
•
•
This is how it would look like if you asked me to rewrite it:
(no NULL-pointer check included).
c Syntax (Toggle Plain Text)
void reverse(char *p) { char *q = p + strlen(p) - 1; for(; p < q; p++, q--) { char c = *p; *p = *q; *q = c; } }
1
•
•
•
•
I would do this a bit different:
void reverse(char p[])
{
int len=strlen(p);
for(int i=0, i<len/2; i++)
{
// exchange elements
p[i]^=p[len - i - 1];
p[len - i - 1]^=p[i];
p[i]^=p[len - i - 1];
}
}
void reverse(char p[])
{
int len=strlen(p);
for(int i=0, i<len/2; i++)
{
// exchange elements
p[i]^=p[len - i - 1];
p[len - i - 1]^=p[i];
p[i]^=p[len - i - 1];
}
}
-1
•
•
•
•
Your code snippet can be shortened tux...here it goes:
Try it and do tell me what do you think...???
c++ Syntax (Toggle Plain Text)
void reverse(char p[]) { int len=strlen(p); for(int i=len-1, j=0;j<i; i--, j++) { // exchange elements p[i]^=p[j]^=p[i]^=p[j]; } }
Try it and do tell me what do you think...???
Last edited by csurfer; Oct 19th, 2009 at 12:23 am.
0
•
•
•
•
Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it... 

0
•
•
•
•
All I wanted just reduce the number of local variables.
Your line “p[i]^=p[j]^=p[i]^=p[j];” in most of compilers has “undefined behaviour”.
Just try to swap to integers a = 0x55 and b = 0xAA your way (a ^= b ^= a ^= b
using different compilers.
Your line “p[i]^=p[j]^=p[i]^=p[j];” in most of compilers has “undefined behaviour”.
Just try to swap to integers a = 0x55 and b = 0xAA your way (a ^= b ^= a ^= b
using different compilers. 0
•
•
•
•
>Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it...
Never mind, any input is welcome.
>All I wanted just reduce the number of local variables.
Yes, I see, though the algorithm of reversing the string is basically the same, I hadn't thought of the the fact that I could use XOR-operations to swap the values without having to use a temporary variable.
Nice suggestion
@vitkaodessit: please post your code using code tags the next time.
Never mind, any input is welcome.
>All I wanted just reduce the number of local variables.
Yes, I see, though the algorithm of reversing the string is basically the same, I hadn't thought of the the fact that I could use XOR-operations to swap the values without having to use a temporary variable.
Nice suggestion

@vitkaodessit: please post your code using code tags the next time.
Last edited by tux4life; Oct 19th, 2009 at 10:04 am.
Similar Threads
- reverse the string (C++)
- String reverse (C#)
- Reverse a string (C)
- Reverse String? (Java)
- Reverse a String (VB.NET)
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets



