Check if a string contains a given substring

Problem statement:- Program to check if a string contains a given substring.

Example:-

              Input: Given

                                 String=check if a string contains a particular substring

                                 Search String=contains

             Output: Searching Word is Found. 

            Input: Given

                                 String=check if a string contains a particular substring

                                 Search String=was

             Output: Searching Word is not Found.         

Data requirement:-


   Input Data:- str,str1

  Output Data:-String output

  Additional Data:- i, j, out


Program in C

Here is the source code of the C Program to check if a string contains a given substring.

Code:

#include<stdio.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    gets(str);

    char str1[30];
    printf("Enter your Searching word:");
    gets(str1);

    int out,i,j;

    for (out= 0; str1[out] != '\0'; out++);
    for (i = 0, j = 0; str[i] != '\0' && str1[j] != '\0'; i++)
    {
        if (str[i] == str1[j])
        {
            j++;
        }
        else
        {
            j = 0;
        }
    }
      if(j==out)
      {
          printf("Searching Word is Found.");
      }
      else
         printf("Searching Word is not Found.");
}

Input/Output:
Enter your String:check if a string contains a particular substring
Enter your Searching word:contains
Searching Word is Found.

Program in C++

Here is the source code of the C++ Program to check if a string contains a given substring.

Code:

#include<iostream>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);

    string str1;
    cout<<"Enter your Searching word:";
     getline(cin,str1);

    int out,i,j;

    for (out= 0; str1[out] != '\0'; out++);
    for (i = 0, j = 0; str[i] != '\0' && str1[j] != '\0'; i++)
    {
        if (str[i] == str1[j])
        {
            j++;
        }
        else
        {
            j = 0;
        }
    }
      if(j==out)
      {
          cout<<"Searching Word is Found.";
      }
      else
         cout<<"Searching Word is not Found.";
}

Input/Output:
Enter your String:check if a string contains a particular substring
Enter your Searching word:was
Searching Word is not Found.

Program in Java

Here is the source code of the Java Program to check if a string contains a given substring.

Code:

import java.util.Scanner;
public class FindWord {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
String str1;
System.out.println("Enter your Searching word:");
str1=cs.nextLine();
int out,i,j;
str+='\0';
str1+='\0';
        
    for (out= 0; str1.charAt(out) != '\0'; out++);
    for (i = 0, j = 0; str.charAt(i)!= '\0' && str1.charAt(j) != '\0'; i++)
    {
        if (str.charAt(i) == str1.charAt(j))
        {
            j++;
        }
        else
        {
            j = 0;
        }
    }
      if(j==out)
      {
      System.out.print("Searching Word is Found.");
      }
      else
      System.out.print("Searching Word is not Found.");
      cs.close();
}
}

Input/Output:
Enter your String:
Check if a string contains a given substring
Enter your Searching word:
given
Searching Word is Found.

Program in Python

Here is the source code of the Python Program to check if a string contains a given substring.

Code:

str=input("Enter Your String:")
str1=input("Enter your Searching word:")
out = 0
i=0
j=0
while out< len(str1):
    for i in range(len(str)):
        for j in range(len(str1)):
            if (str[i] == str1[j]):
                j+=1
            else:
                j=0
    out+=1
if(j==out):
    print("Searching word is Found.")
else:
    print("Searching Word is not Found.")

Input/Output:
Enter Your String:string practice questions in python
Enter your Searching word:python
Searching word is Found.

Post a Comment

0 Comments