-
Notifications
You must be signed in to change notification settings - Fork 1
/
program3.c
100 lines (94 loc) · 2.27 KB
/
program3.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
95
96
97
98
99
// find words that contain at least a vowel from a file and store it in a table and then find the no. of occurances that particular word in the file by taking word from the table.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int check_v(char a){
int chk=0;
a = tolower(a);
if(a=='a' ||a=='e' ||a=='i'||a=='o'||a=='u')
chk=1;
return chk;
}
void file_content(char *a){
FILE *r;
char ch;
r = fopen(a,"r");
while((ch=getc(r))!=EOF){
printf("%c",ch);
}
fclose(r);
}
void main(){
char arr[100][50];
int i=0;
FILE *ptr;
ptr = fopen("test1.txt","r");
if(ptr == NULL){
printf("\n\nCreate a file 'test1.txt' and run program again\n\nTERMINATING\n\n");
exit(1);
}
printf("\nCONTENT OF FILE : ");
file_content("test1.txt");
// read file and store words in an array
while(1){
char r = getc(ptr);
int k=0;
r=tolower(r);
while((r>='a')&&(r<='z')&&(r!=EOF)){
arr[i][k++] = r;
r=getc(ptr);
r=tolower(r);
}
arr[i][k]=0;
if(feof(ptr)){
break;
}
i++;
}
// Now the words from the file have been stored in an array arr[], size is i
// For each word in arr[], perform the following check.
// Check if the word has a vowel in it
// if vowel exists, check whether the word exists in the vow[] array
// if not, then add the word to the list, otherwise, continue
int j,len,n,chk,m_chk,vc=0,cs;
char vow[100][50];
for(j=0;j<i;j++){
// For each word check if it consists of a vowel.
len=strlen(arr[j]);
chk=0;
m_chk=0;
for(n=0;n<len;n++){
chk = check_v(arr[j][n]);
if(chk==1)
break;
}
// if vowel exists, search for word in vow[] array
if(chk==1){
for(n=0;n<=vc;n++){
if(strcmp(arr[j],vow[n])==0)
m_chk=1;
if(m_chk==1)
break;
}
}
// if word does not exists i.e., m_chk=0, add the word in the list vow[]
if((chk == 1) && (m_chk==0)){
strcpy(vow[vc],arr[j]);
vc++;
}
}
printf("\nWords with vowels are : \n");
// Now vow[] consists of 1 instance of word that has a vowel in it and arr[] consists of all words from file
// for each word in vow, check for that word in arr[]
for(j=0;j<vc;j++){
chk=0;
for(n=0;n<=i;n++){
if(strcmp(arr[n],vow[j])==0)
chk++;
}
// Now we have the word and no of instances of that word.
printf("%s \t %d\n",vow[j],chk);
}
fclose(ptr);
}