-
Notifications
You must be signed in to change notification settings - Fork 0
/
search string using files.c
88 lines (69 loc) · 1.61 KB
/
search string using files.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
void read(char asentence[][100],int n);
void display(char bsentence[][100],int n);
int check(char bsentence[],char key[],char key1[]);
void main()
{
int n;
char sentence[100][100];
printf("Enter the number of lines\n");
scanf("%d",&n);
read(sentence,n);
}
void read(char asentence[][100],int n)
{
FILE *fp1,*fp2;
fp1=fopen("searchin.txt","r");
fp2=fopen("searchop.txt","w");
int tp;
char key[10],key1[10];
printf("Enter the key word that you want to search for\n");
scanf("%s",key);
printf("Enter the key word that you want to replace for\n");
scanf("%s",key1);
while(!feof(fp1))
{
for(int i=0;i<n;i++)
{
fgets(asentence[i],100,fp1);
tp=check(asentence[i],key,key1);
if(tp==1)
{
fputs(asentence[i],fp2);
puts(asentence[i]);
}
}
}
}
int check(char bsentence[],char key[],char key1[])
{
int i=0,j=1,k=0,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;
bsentence[i]=key1[k];
k++;
break;
}
else
{
j=0;
}
}
i++;
}
return tp;
}