/* Program to
reverse the alternate words of a string */
#include<stdio.h>
#include<string.h>
void reverse(char
rev[],int n);
int main()
{
char
str[100],rev[10];
int
len,i,j,count;
printf("\n
Enter a string:");
gets(str);
len=strlen(str);
printf("\n
Converted string is:\n");
for(i=0;i<len;i++)
{
printf("%c",str[i]);
if(str[i]==' ')
{
j=i;
count=0;
while(str[++j]!=' ')
{
rev[count]=str[j];
count ++;
}
reverse(rev,count);
i=j;
putc(' ',stdout);
}
}
return 0;
}
void reverse(char
rev[],int n)
{
int i;
for(i=n-1;i>=0;i--)
printf("%c",rev[i]);
}
INPUT/OUTPUT:
Enter a string:Program to reverse the alternate words of a string
Converted string is:
Program ot reverse eht alternate sdrow of a string
No comments:
Post a Comment