#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverseStr(char *input,int start,int end);
int main()
{
char *input=NULL;
int length=100, end=0, start=0, error=0;
input = (char *) malloc (length + 1);
if(input == NULL)
{
printf("malloc error\n");
exit(1);
}
printf("Please give the input string\n");
error=getline(&input,&length,stdin);
if(error == -1)
{
printf("error in getline\n");
exit(1);
}
length=strlen(input);
input[length-1]='\0';
end=length-2;
printf("Currently the string:[%s]\n",input);
reverseStr(input,start,end);/*reverse the string*/
for(start=0;start < end; )/*Finds the points of first and last word*/
{
if( input[start] != ' ' )
{
start++;
}
if( input[end] != ' ' )
{
end--;
}
if( (input[start] == ' ') && (input[end]== ' '))
{
break;
}
}
reverseStr(input,start+1,end-1);/*reverse the string without last and first word*/
reverseStr(input,0,start-1);/*reverse the string first word*/
reverseStr(input,end+1,length-2);/*reverste the last letter*/
printf("Now the string:[%s]\n",input);
free (input);
return 0;
}
void reverseStr(char *input,int start,int end)
{
char temp;
while(start<end)
{
temp=input[end];
input[end]=input[start];
input[start]=temp;
start++;
end--;
}
return;
}