-
Notifications
You must be signed in to change notification settings - Fork 0
/
search a string.c
70 lines (65 loc) · 1.34 KB
/
search a string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
void read(char asentence[][100],int n);
void display(char bsentence[][100],int n);
int check(char bsentence[],char key[]);
void main()
{
int n;
char sentence[100][100];
printf("Enter the number of lines\n");
scanf("%d",&n);
read(sentence,n);
display(sentence,n);
}
void read(char asentence[][100],int n)
{
for(int i=-1;i<n;i++)
{
gets(asentence[i]);
}
}
void display(char bsentence[][100],int n)
{
int tp;
char key[10];
printf("Enter the key word that you want to search for\n");
scanf("%s",key);
for(int i=0;i<n;i++)
{
tp=check(bsentence[i],key);
if(tp==1)
{
puts(bsentence[i]);
}
}
}
int check(char bsentence[],char key[])
{
int a[10],i=0,j=1,tp=0;
while(bsentence[i]!='\0')
{
tp=0;
if(bsentence[i]==key[0])
{
while(key[j]!='\0')
{
if(bsentence[i+j]!=key[j])
{
break;
}
j++;
}
if(key[j]=='\0')
{
tp=1;
break;
}
else
{
j=0;
}
}
i++;
}
return tp;
}