C-String reverse

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
tux4life tux4life is offline Offline Apr 30th, 2009, 6:27 pm |
0
A small function which reverses a c-string by just swapping the elements ...

Usage::
  1. char[] str = "hello";
  2. reverse(str); // str now contains "olleh"
Quick reply to this message  
C++ Syntax
  1. /**********************************
  2. * Written by Mathias Van Malderen *
  3. **********************************/
  4.  
  5. void reverse(char p[])
  6. {
  7. int len=strlen(p);
  8. char t;
  9. for(int i=(--len), j=0; i>len/2; i--, j++)
  10. {
  11. // exchange elements
  12. t=p[i];
  13. p[i]=p[j];
  14. p[j]=t;
  15. }
  16. }
0
MosaicFuneral MosaicFuneral is offline Offline | May 1st, 2009
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.
 
0
tux4life tux4life is offline Offline | May 7th, 2009
>_strrev() is generally provided in most compilers.
But not on all
 
0
tux4life tux4life is offline Offline | Jul 30th, 2009
This is how it would look like if you asked me to rewrite it:
  1. void reverse(char *p)
  2. {
  3. char *q = p + strlen(p) - 1;
  4.  
  5. for(; p < q; p++, q--)
  6. {
  7. char c = *p;
  8. *p = *q;
  9. *q = c;
  10. }
  11. }
(no NULL-pointer check included).
 
-3
rajeshvari rajeshvari is offline Offline | Oct 10th, 2009
make program in c language by using for loop to show
3
323
32123
323
3
 
1
vitkaodessit vitkaodessit is offline Offline | Oct 18th, 2009
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];
}
}
 
-1
csurfer csurfer is offline Offline | Oct 19th, 2009
Your code snippet can be shortened tux...here it goes:
  1. void reverse(char p[])
  2. {
  3. int len=strlen(p);
  4. for(int i=len-1, j=0;j<i; i--, j++)
  5. {
  6. // exchange elements
  7. p[i]^=p[j]^=p[i]^=p[j];
  8. }
  9. }

Try it and do tell me what do you think...???
Last edited by csurfer; Oct 19th, 2009 at 12:23 am.
 
0
csurfer csurfer is offline Offline | Oct 19th, 2009
Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it...
 
0
vitkaodessit vitkaodessit is offline Offline | Oct 19th, 2009
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.
 
0
tux4life tux4life is offline Offline | Oct 19th, 2009
>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.
Last edited by tux4life; Oct 19th, 2009 at 10:04 am.
 
 

Message:


Similar Threads
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC