×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to find a specific word ends with a specific character in the string

Here, we are going to learn how to find a specific word ends with a specific character in the string in C programming language?
Submitted by Nidhi, on July 20, 2021

Problem statement

Read a string and a character from the user and check words is ending is the specific character within the string using C program.

C program to find a specific word ends with a specific character in the string

The source code to find a specific word ends with a specific character in the string is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find a specific word ends
// with specific character in string

#include <stdio.h>
#include <string.h>

int main()
{
    char str[32];
    char ch = 's';

    int len = 0;

    int i = 0;
    int j = 0;
    int t = 0;
    int f = 0;

    printf("Enter string: ");
    scanf("%[^\n]s", str);

    len = strlen(str);

    str[len] = ' ';

    for (t = 0, i = 0; i < len; i++) {
        if ((str[i] == ' ') && (str[i - 1] == ch)) {
            for (j = t; j < i; j++) {
                if (f == 0)
                    printf("Words are: \n");
                printf("%c", str[j]);
                f = 1;
            }
            t = i + 1;
            printf("\n");
        }
        else {
            if (str[i] == ' ')
                t = i + 1;
        }
    }

    if(f == 0)
        printf("Words not found ending with '%c'\n", ch);

    return 0;
}

Output

RUN 1:
Enter string: This is India.
Words are: 
This
is

RUN 2:
Enter string: hello guys how are you?
Words are: 
guys

RUN 3:
Enter string: Is are am.
Words are: 
Is

Explanation

In the main() function, we read a string from the user and check words within string ending with character 's' and printed the result on the console screen.

C String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.