Program Discussion :: Strings
85 / 60
Given a pattern and a text, write a program to check whether the pattern is present in the text
Answer:
#include
#include
using namespace std;
int main()
{
int i,j,temp;
char str[100]={"This is a pattern matching"};
char substr[20]={"pattern"};
for(i=0;str[i]!='\0';i++)
{
j=0;
if(str[i]==substr[j])
{
temp=i+1;
while(str[i]==substr[j])
{
i++;
j++;
}
if(substr[j]=='\0')
{
cout
Asked In ::
Language:
Arvind Singh
7 Jul, 2017 9:30 AM
#include<stdio.h>
int main()
{
int i,j,temp;
char str[100]={"This is a pattern matching"};
char substr[20]={"pattern"};
for(i=0;str[i]!='\0';i++)
{
j=0;
if(str[i]==substr[j])
{
temp=i+1;
while(str[i]==substr[j])
{
i++;
j++;
}
if(substr[j]=='\0')
{
printf("The substring is present in given string at position %d\n",temp);
exit(0);
}
else
{
i=temp;
temp=0;
}
}
}
if(temp==0)
printf("The substring is not present in given string\n");
return 0;
}
Language:
Disha
7 Jul, 2017 9:30 AM
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i,j,temp;
char str[100]={"This is a pattern matching"};
char substr[20]={"pattern"};
for(i=0;str[i]!='\0';i++)
{
j=0;
if(str[i]==substr[j])
{
temp=i+1;
while(str[i]==substr[j])
{
i++;
j++;
}
if(substr[j]=='\0')
{
cout<<"The substring is present in given string at position "<<temp<<"\n";
exit(0);
}
else
{
i=temp;
temp=0;
}
}
}
if(temp==0)
cout<<"The substring is not present in given string\n";
return 0;
}