/*
Input: This is a test string
Output: string test a is This
*/
#include<iostream.h>
#include<string.h>
int reverse(char *str,int s,int e)
{
char temp;
for(int i=0 ; i<(e-s+1)/2 ; i++)
{
temp = str[i+s];
str[i+s]=str[e-i];
str[e-i]=temp;
}
}
int main()
{
char str[]="This is a test string";
int len = strlen(str);
int start=0,end;
reverse(str,0,len-1);
for(int i=0; i<=len ; i++)
{
if(str[i]==' ' || str[i]=='\0')
{
end = i-1;
reverse(str,start,end);
start = i+1;
}
}
cout<<str;
}
No comments:
Post a Comment