Below code reverse a word given by user input example.
input : This is code Review
output : siht si edoc weiveR
steps
step 1: check for whitespace if found set the start and end position and pass to reverseword
step 2: check for null terminator and set start and end position pass to reverseword
#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void reverseword(int start,int end,char *input)
{
char temp;
int i;
while(end > start)
{
temp=input[start];
input[start]=input[end];
input[end]= temp;
start++;
end--;
}
}
int main()
{
char input[MAX];
scanf("%[^\n]",input);
int pos;
int start=0,end=0;
for(pos=0 ; input[pos] ; ++pos)
{
if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found
{
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
}
if(input[pos+NEXT]=='\0' && pos>MIN_CHAR) // case for last world
{
end =pos;
reverseword(start,end,input);
}
}
}