-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1.c
94 lines (73 loc) · 1.77 KB
/
Q1.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
89
90
91
92
93
94
#include<stdio.h>
#include<stdlib.h>
//#include<string.h>
int Occur_of_Sub_String(char* arr,FILE* fp,int size)
{
int occur = 0; //counter to return number of occurences
char ch=getc(fp);
//printf("%d",arr[0]);
while(ch!=EOF)
{
// printf("%d",ch);
if(ch==(int)arr[0])//check for first match
{
for (int i=1;i<size;i++)
{
if((ch=getc(fp))!=arr[i])
{
break;
}
if((ch=getc(fp))==arr[i])
{
if(i==size-1)//for complete matching
{
occur++;
}
}
}
}
ch=getc(fp);
}
return occur;
}
int main()
{
int num;
int size;
FILE* fptr;
char* arr,c; //for substring
//input size of substring
printf("Enter the size of substring\n\n");
scanf("%d",&size);
arr =(char*) malloc(sizeof(char) );// allocation of memory
if(arr!=NULL)
{
printf("Size is allocated Please Enter Substring\n\n");
}
if(arr==NULL)
{
printf("Memory is not allocated \n\n");
}
int i=0;
for(int i=0;i<size;++i)
{
char var;
scanf("%d",&var);
arr[i++]=var;
}
for(int i=0;i<size;i++)
{
char var=arr[i];
printf("%s",var);
}
fptr = fopen("D:\\Q1.txt", "r") ;
if (fptr == NULL) //base case
{
printf("File does not exist (' | ')");
return 1;
}
printf("Value of Occurences=%d",Occur_of_Sub_String(arr,fptr,size) );
fclose(fptr);
free(arr); //deallocate memory
return 0;
}