Find words ending with given characters(suffix)

Problem statement:- Program to Find words ending with given characters(suffix).

Example:-

              Input: Given

                                 String=string java c Programing

                                       ch= g

             Output: All the words ending with g are:string Programing

            Input: Given

                                String=cat bat dog rat

                                       ch= t

             Output: All the words ending with t are:cat bat rat

Data requirement:-


   Input Data:- str

  Output Data:-
sub_str

  Additional Data:- 
in, out, p, len1, len

Program in C

Here is the source code of the C Program to Find words ending with given characters(suffix).

Code:

#include<stdio.h>
#include<string.h>
main()
{
    int in=0,out=0,p=0,len1;
    char str[100]={0},sub_str[100][100]={0},ch;
    printf("Enter your String:");
    gets(str);

    printf("Enter the Character:");
    scanf("%c",&ch);

    //splitting Input String into sub string
    while(str[p]!='\0')
    {
        out=0;
        while(str[p]!=' '&&str[p]!='\0')
        {
            sub_str[in][out]=str[p];
            p++;
            out++;
        }
        sub_str[in][out]='\0';
        in++;
        if(str[p]!='\0')
        {
            p++;
        }
    }
    int len=in;
    printf("All the words ending with %c are:",ch);

        for(in=0;in<len;in++)
        {
            len1=strlen(sub_str[in]);
            if(sub_str[in][len1-1]==ch)
            {
                printf("%s ",sub_str[in]);
            }
        }
}

Input/Output:
Enter your String:string java c Programing
Enter the Character:g
All the words ending with g are:string Programing

Program in C++

Here is the source code of the C++ Program to Find words ending with given characters(suffix).

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
   int in=0,out=0,len1,p=0;
    char sub_str[100][100]={0},ch;
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    cout<<"Enter the Character:";
    cin>>ch;

    //splitting Input String into sub string
    while(str[p]!='\0')
    {
        out=0;
        while(str[p]!=' '&&str[p]!='\0')
        {
            sub_str[in][out]=str[p];
            p++;
            out++;
        }
        sub_str[in][out]='\0';
        in++;
        if(str[p]!='\0')
        {
            p++;
        }
    }
    int len=in;
    cout<<"All the words ending with "<<ch<<" are:";

        for(in=0;in<len;in++)
        {
            len1=strlen(sub_str[in]);
            if(sub_str[in][len1-1]==ch)
            {
                cout<<sub_str[in]<<" ";
            }
        }
}

Input/Output:
Enter your String:cat bat dog rat
Enter the Character:t
All the words ending with t are:cat bat rat

Program in Java

Here is the source code of the Java Program to Find words ending with given characters(suffix).

Code:

import java.util.Scanner;
public class EndingWithGivenChar {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
System.out.println("Enter the Character:");
String ch=cs.nextLine();
//splitting Input String into sub string
String[] sub_str=str1.split("\\s");
int in;
int len=sub_str.length;
System.out.print("All the words ending with "+ch+" are:");

        for(in=0;in<len;in++)
        {
            if(sub_str[in].endsWith(ch))
            {
                System.out.print(sub_str[in]+" ");
            }
        }
cs.close();
}
}

Input/Output:
Enter your String:
wood end left tight
Enter the Character:
d
All the words ending with d are:wood end 

Program in Python

Here is the source code of the Python Program to Find words ending with given characters(suffix).

Code:

str=input("Enter Your String:")
ch=input("Enter the Character:")
sub_str=str.split(" ")
print("All the words ending with ",ch," are:")
for inn in range(0,len(sub_str)):
    if sub_str[inn].endswith(ch):
        print(sub_str[inn],end=" ")

Input/Output:
Enter Your String:string program in python
Enter the Character:n
All the words ending with  n  are:
in python 

Post a Comment

0 Comments