/*reverse .c */ /*reverse the content of a string */ /*author: Weiwei Chen */ /*Date: Nov. 21. 2010 */ #define SLEN 128 /*the maximum length of the string*/ #include int getStrLength(char str[SLEN]) { char* p = str; int i = 0; while(*p++) { i ++; } return i; } void reverse(char str[SLEN], int size) { int mid, i; char tmp; mid = size / 2; for(i = 0; i < mid; i ++) { tmp = str[i]; str[i] = str[size - 1 - i]; str[size - 1 - i] = tmp; } return; } int main() { char mystring[SLEN], len; printf("Please input a string: "); scanf("%127s", mystring); /*get the input string */ len = getStrLength(mystring); /*get the length of the string */ reverse(mystring, len); /*reverse the content */ printf("The reversed string is: %s\n", mystring); return 0; }