Thursday, November 27, 2014

C program to Search for a string in other string

/*...............................................................................................................................
File : rec19.c
Program: Search for a string in other string
Author : sujithbaby440@gmail.com
Date : 15-11-14
.................................................................................................................................*/
#include<stdio.h>
#include<string.h>
int StrInStr(char s[], char t[]);
main ()
{
int num=1;
char s[20],t[20];
system("clear");
printf("Program to find a string inside another string\n");
printf("Enter the string\n");
scanf("%s",s);
printf("Enter the serch string\n");
scanf("%s",t);
num=StrInStr(s,t);
if(num==1){
printf("string is present\n");
}else{
printf("string is not present\n");
}
}
int StrInStr(char S[],char T[])
{
int i,j,k,n,temp=0;
i=0;
j=0;
k=0;
n=0;
while(S[n]!='\0'){
n++;
}
while(k<n){
j=k;
while(T[i]==S[j]){
i++;
j++;
temp=1;
}
k++;
}
if((T[i]=='\0')&&(temp=1)){
return 1;
}else{
return -1;
}
}

OUTPUT 1
Program to find a string inside another string
Enter the string
sundaymondaytuesday
Enter the search string
monday
string is present
[141740@localhost ~]$

OUTPUT 2
Program to find a string inside another string
Enter the string
searching
Enter the search string
search
string is present
[141740@localhost ~]$

No comments:

Post a Comment