C Program to Print the Words Ending with Letter S

This is a C Program to print the words ending with letter s.

Problem Description

This program takes a string as input and print the words ending with letter s.

Problem Solution

1. Take a string as input.
2. Find the blank space in the string and then check if the previous letter is ‘s’ or not.
3. If it is ‘s’ then print the corresponding word.

Program/Source Code

Here is source code of the C Program to print the words ending with letter s. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /* 
  3.  * C Program to Print the Words Ending with Letter S
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. char str[100];
  9.  
  10. void main()
  11. {
  12.     int i, t, j, len;
  13.  
  14.     printf("Enter a string : ");
  15.     scanf("%[^\n]s", str);
  16.  
  17.     len = strlen(str);
  18.  
  19.     str[len] = ' ';
  20.  
  21.     for (t = 0, i = 0; i < strlen(str); i++)
  22.     {
  23.         if ((str[i] == ' ') && (str[i - 1] == 's'))
  24.         {
  25.             for (j = t; j < i; j++)
  26.                 printf("%c", str[j]);
  27.             t = i + 1;
  28.             printf("\n");
  29.         }
  30.         else
  31.         {
  32.             if (str[i] == ' ')
  33.             {
  34.                 t = i + 1;
  35.             }
  36.         }
  37.     }
  38. }
Program Explanation

1. Take a string as input and store it in the array str[].
2. Find its length and add a blank space to the input string at last.
3. Using for loop find the blank space in the string and check whether its previous letter is ‘s’ or not.
4. If it is ‘s’ then print the corresponding word. Otherwise repeat the steps 3-4 until the last element of the string.

advertisement
advertisement
Runtime Test Cases
 
Enter a string : Welcome to Sanfoundry's C Programming Class, Welcome Again to C Class !
Sanfoundry's
Class

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.