C program to check substring or not

Before writing C program to check substring or not using arrays lets understand what is a substring. we can say a string1 is substring of  sring2 if string1 is contained in string2. eg:  String1=”Hello”; and String2=”Hello World”. we can see that the word “Hello” is contained in “Hello World” string. So String1 is substring of String2.

To check substring or not using arrays we store both strings in a character arrays  i.e. char[] because it helps to compare characters of two string.

c program to check substring or not using arrays for a given string
c program to check substring or not using arrays for a given string

write a C program to check substring or not using arrays

write a C program to check substring or not using arrays

#include<stdio.h>
#include<conio.h>
void main()
{
char str[80],substr[10];
int count1=0,count2=0,i,j,flag;
clrscr();
printf(“—To find a given string is substring or not using arrays—“);
puts(“Enter a string:”);
gets(str);
puts(“Enter substring:”);
gets(substr);
while (str[count1]!=’\0′)
count1++;
while (substr[count2]!=’\0′)
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=substr[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts(“Given String contains substring”);
else
puts(“String does not contains substring”);
getch();
}

output of C program to check substring or not using arrays for given string
output of C program to check substring or not using arrays for given string

 

write a C program to check substring or not using arrays ,put logic in a function

write a C program to check substring or not using arrays,put logic in a function

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[80],s2[10];
clrscr();
printf(“—To find a given string is substring or not using arrays—“);
puts(“Enter a string:”);
gets(s1);
puts(“Enter substring:”);
gets(s2);
Check_Substring(s1,s2);
getch();
}

void Check_Substring(char str[80],char substr[10] )
{
int count1=0,count2=0,i,j,flag;
while (str[count1]!=’\0′)
count1++;
while (substr[count2]!=’\0′)
count2++;
for(i=0;i<=count1-count2;i++)
{
for(j=i;j<i+count2;j++)
{
flag=1;
if (str[j]!=substr[j-i])
{
flag=0;
break;
}
}
if (flag==1)
break;
}
if (flag==1)
puts(“Given String contains substring”);
else
puts(“String does not contains substring”);
}

Other programs:

Static variable scope
Register variable scope
External variable scope
Automatic variable scope
amicable number
Prime number
Perfect number
Palindrome number
Strong number
Find Factorial 
Biggest Number
Average of n numbers
convert decimal number to octal
decimal number to hexadecimal
decimal number to binary
simple interest
compound interest
Add sparce matrices
Add matrices
 file copying
Palindrome string
Reverse string
String length
Replace characters in string
substring or not using arrays
First occurrence of character in string
Last occurrence of character in string
Convert to uppercase
Convert to lowercase
Concatenate strings 
Compare strings
Generate Employee payslips
Count students scoring marks>50%
convert weight into grams

 

One thought on “C program to check substring or not”

Leave a Reply

Your email address will not be published. Required fields are marked *