Problem statement:- Program to Find words Starting with given characters(Prefix).
Example:-
Input: Given
String=bat bed bag no
ch= b
Output: All the words starting with b are:bat bed bag
Input: Given
String=dirt tight dog dove
ch=d
Output: All the words ending with t are:dirt dog dove
Data requirement:-
Input Data:- str
Output Data:-sub_str
Additional Data:- in, out, p, len
Program in C
Here is the source code of the C Program to Find words Starting with given characters(Prefix).
Code:
#include<stdio.h>
#include<string.h>
main()
{
int in=0,out=0,p=0;
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 starting with %c are:",ch);
for(in=0;in<len;in++)
{
if(sub_str[in][0]==ch)
{
printf("%s ",sub_str[in]);
}
}
}
Enter your String:bat bed bag not
Enter the Character:b
All the words starting with b are:bat bed bag
Program in C++
Here is the source code of the C++ Program to Find words Starting with given characters(Prefix).
Code:
#include<iostream>
#include <cstring>
using namespace std;
main()
{
int in=0,out=0,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 starting with "<<ch<<" are:";
for(in=0;in<len;in++)
{
if(sub_str[in][0]==ch)
{
cout<<sub_str[in]<<" ";
}
}
}
Enter your String:dirt tight dog dove
Enter the Character:d
All the words starting with d are:dirt dog dove
Program in Java
Here is the source code of the Java Program to Find words Starting with given characters(Prefix).
Code:
import java.util.Scanner;
public class StartingWithGivenChar {
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 Starting with "+ch+" are:");
for(in=0;in<len;in++)
{
if(sub_str[in].startsWith(ch))
{
System.out.print(sub_str[in]+" ");
}
}
cs.close();
}
}
Enter your String:
find end egg eat
Enter the Character:
e
All the words Starting with e are:end egg eat
Program in Python
Here is the source code of the Python Program to Find words Starting with given characters(Prefix).
Code:
str=input("Enter Your String:")
ch=input("Enter the Character:")
sub_str=str.split(" ")
print("All the words starting with ",ch," are:")
for inn in range(0,len(sub_str)):
if sub_str[inn].startswith(ch):
print(sub_str[inn],end=" ")
Enter Your String:string program in python
Enter the Character:p
All the words starting with p are:
program python
0 Comments
Please do not Enter any spam link in the comment box