Q:

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

belongs to collection: C String Programs

0

C program to check two strings are anagram or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

Program:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to split the string using the strtok() f... >>
<< C program to check two strings are anagram or not...