/*Program to reverse the given string*/ #include #define MAXCH 100 void revstr(char *); int main() { char arr[MAXCH]; *arr=' '; *(char *)(arr+1)='\0'; while(strcmp(arr,"")!=0) { printf("Enter the string (null string to terminate): "); fgets(arr,MAXCH,stdin); *(char *)(arr+strlen(arr)-1)='\0'; puts(arr); revstr(arr); if(strcmp(arr,"")!=0) puts("Reversed string: "); puts(arr); } return 0; } /*Usage: revstr(char *str) Return value: void Arguments: pointer to array of characters Reverses the order of the array contents*/ /*Assumed that string is null terminated*/ void revstr(char *arr) { char *p,*q; char *tmp=(char *)(malloc(sizeof(char)*strlen(arr))); for(p=(char *)(arr+strlen(arr)-1),q=tmp;p>=arr;p--,q++) *q=*p; *(char *)(q+strlen(arr))='\0'; strcpy(arr,tmp); }