Contact Learn C
Copy Program Share Program

Program 329 : Find all Words Ended with given Character

Find all Words Ended with given Character
Method I:Split Words in Sentence and then Check the End of String for given Character
#include<stdio.h>
#include<string.h>
main()
{
 int i=0,j=0,k=0,len;
 char str[100],character,substr[100][100]={0};
 printf("Enter String\n");
 gets(str);
 printf("Enter the Character\n");
 scanf("%c",&character);
 while(str[k]!='\0')//for splitting sentence at spaces
    {
        j=0;
        while(str[k]!=' '&&str[k]!='\0')
        {
            substr[i][j]=str[k];
            k++;
            j++;
        }
        substr[i][j]='\0';
        i++;
        if(str[k]!='\0')
        {
            k++;
        }        
    }
 len=i;
 for(i=0;i<len;i++)//After splitting checking the end of each word is given Character or not
    {
     int substrLen=strlen(substr[i]);
     if(substr[i][substrLen-1]==character)
     printf("%s\n",substr[i]);
    } 
}

Method II:Check the Character at the end of word while traversing
#include<stdio.h>
#include<string.h>
main()
{
 int i,j,start=0;
 char str[100],character;
 printf("Enter String\n");
 gets(str);
 printf("Enter the Character\n");
 scanf("%c",&character);
 for(i=0;i<=strlen(str);i++)
 {
  if((str[i]==' ' && str[i-1]==character)||(str[i]=='\0' && str[i-1]==character))
  {
   for(j=start;j<i;j++)
   {
    printf("%c",str[j]);
   }
   printf("\n");
   start=i+1;
  }
  else
  {
   if(str[i]==' ')
   start=i+1;
  }
 }
}
Explanation:

//Coming Soon...

Output:
Find all Words Ended with given Character




Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide