4
Reverse
- #include <string.h>
- char *reverse( char *s )
- {
- char *p, *q;
- for ( p = s, q = s+strlen(s)-1; q > p; p++, q-- ) {
- char c = *p;
- *p = *q;
- *q = c;
- }
- return s;
- }
- #if defined( STANDALONE )
- #include <iostream>
- void test( char *s )
- {
- // printing s and reverse( s ) in one statement calls reverse first!
- std::cout << "[" << s << "]<->[";
- std::cout << reverse( s ) << "]\n";
- }
- int main() {
- // don't use char* (read-only)
- char s1[] = "123";
- char s2[] = "12";
- char s3[] = "";
- test( s1 );
- test( s2 );
- test( s3 );
- }
- #endif
Comments