Ok, I made an example. You should be inspired by it and do the logic (by modifying the code) to fit your code.
See my comments.
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char sentence[50] = "My name, is George Samaras";
// to be at the end of the word
char* end = &sentence[strlen(sentence)];
// like i counter in for loop
char* worker;
// 1 if the word that we print now is
// not the first word of the sentence
// This is a flag in other words
int notFirstWord = 1;
// a space, dot, etc.
char separator;
// to be at the start of the word
char* start = &sentence[strlen(sentence)-1];
// while we have something to read
while(*end != sentence[0])
{
// while start point to a letter
// (could use isalpha from ctype.h)
while(*start != ' ' && *start != '.' && *start != ',')
{
// decrease start pointer until it reaches something
// that's not a letter
start--;
// if it is the 1st word
// change the variable notFirstWord
if(*start == sentence[0])
{
notFirstWord = 0;
break;
}
}
// start points to the separator
separator = *start;
// if it is not the first word, then we need to move start one position
// to point to the 1st letter of the word. Print until we find the end
// of the word
for(worker=start+notFirstWord ; worker != end ; worker++)
printf("%c", *worker);
if(notFirstWord)
printf("%c", separator);
// update end pointer
end = start;
// go the previous character
start--;
}
return 0;
}
Now, I got to go to a party, so enjoy coding without me around!! Ciao!!