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.

advertisement
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
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 Reference Books in C Programming, Data-Structures and Algorithms

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.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn