Friday, October 30, 2009

Chap9[F]c Reversing strings

Write a program to reverse the strings stored in the following array of pointers to strings:
char *s[ ] = {
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
} ;

Hint: Write a function xstrrev ( string ) which should reverse the contents of one string. Call this function for reversing each string stored in s.


void main()
{
char *s[]={
"To err is human...",
"But to really mess things up...",
"One needs to know C!!"
};


int i;



for(i=0;i<3;i++)
xstrrev(s[i]);

for(i=0;i<3;i++)
printf("\n%s",s[i]);

}

xstrrev(char *m)
{
char *temp1;
char *temp2;
char *clear;

int i,len,j;
*clear='\0';
len=strlen(m)-1;

for(i=0,j=len;;i++,j--)
{
printf("\n b4 *(m+i) is %c",*(m+i));
*temp1=*(m+i);
*temp2=*(m+j);
*(m+i)=*clear;
*(m+j)=*clear;
*(m+i)=*temp2;
*(m+j)=*temp1;

/* printf("\n i is %d, j is %d",i,j); */
if((j-i)==1||j==i)
break;
printf("\n After *(m+i) is %c",*(m+i));


}

}

No comments:

Post a Comment