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));
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment