Given a sentence of words, print the words which ends with character 's'.
Implementation:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int flag=0;
/* Recursive Function to print the words that end with character 's' */
void printword(char *s,int n)
{
if(*(s+n)!=' ' && n>=0)
{
printword(s,n-1);
printf("%c",*(s+n));
flag=1;
}
else
return;
}
int main(void) {
char *s;
int i=0;
/* Allocate memory for a string */
s=(char*)malloc(sizeof(char));
/* Get the sentence from the user */
gets(s);
/* Until we reach the end of the sentence */
for(i=0;i<=strlen(s);i++)
{
/* Check if there is a space character or null character */
if(*(s+i)==' ' || *(s+i)=='\0')
{
/* If so , check whether the word had end with 's' */
if(*(s+i-1)=='s')
/* If so, call the recursive function to print the word */
printword(s,i-1);
/* If there is a word, print a space character */
if(flag)
{
printf(" ");
flag=0;
}
}
}
return 0;
}