4

Reverse

  1. #include <string.h>
  2.  
  3. char *reverse( char *s )
  4. {
  5.     char *p, *q;
  6.  
  7.     for ( p = s, q = s+strlen(s)-1; q > p; p++, q-- ) {
  8.         char c = *p;
  9.         *p = *q;
  10.         *q = c;
  11.     }
  12.     return s;
  13. }
  14.  
  15. #if defined( STANDALONE )
  16.  
  17. #include <iostream>
  18.  
  19. void test( char *s )
  20. {
  21.     // printing s and reverse( s ) in one statement calls reverse first!
  22.     std::cout << "[" << s << "]<->[";
  23.     std::cout << reverse( s ) << "]\n";
  24. }
  25.  
  26. int main() {
  27.     // don't use char* (read-only)
  28.     char s1[] = "123";
  29.     char s2[] = "12";
  30.     char s3[] = "";
  31.  
  32.     test( s1 );
  33.     test( s2 );
  34.     test( s3 );
  35. }
  36.  
  37. #endif

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].