In: Computer Science
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)!
Program works just need to modify it to take in input from a text file and output the results in an output file.
________________________________________________________________________________________________
#include <stdio.h>
#include <string.h>
// Maximum string size
#define MAX_SIZE 1000
int countOccurrences(char * str, char * toSearch);
int main()
{
char str[MAX_SIZE];
char toSearch[MAX_SIZE];
char ch;
int count,len,a[26]={0},p[MAX_SIZE]={0},temp;
int i,j;
//Take user's input
printf("Enter any string : ");
gets(str);
//store length of string
len=strlen(str);
//array "a" contains count of every character
for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z')
a[str[i]-'a']++;
}
//output the result
for(i=0;i<26;i++)
{
if(a[i]!=0)
{
ch=i +'a';
printf("%c occurs %d times\n",ch,a[i]);
}
}
//array "p" contains count of length of every word
for(i=0;i<len;i++)
{
temp=0;
while(i<len&&str[i]!=' ')
{
temp++;
i++;
}
p[temp]++;
}
//output the result
for(i=0;i<MAX_SIZE;i++)
{
if(p[i]!=0)
{
printf("Word length %d occurs %d times\n",i,p[i]);
}
}
for(i=0;i<len;i++)
{
memset ( toSearch, 0, MAX_SIZE );
temp=0;
while(i<len&&str[i]!=' ')
{
toSearch[temp]=str[i];
temp++;
i++;
}
count = countOccurrences(str, toSearch);
printf("Total occurrences of '%s' is %d\n", toSearch, count);
}
return 0;
}
int countOccurrences(char * str, char * toSearch)
{
int i, j, found, count;
int stringLen, searchLen;
stringLen = strlen(str); // length of string
searchLen = strlen(toSearch); // length of word to be searched
count = 0;
for(i=0; i <= stringLen-searchLen; i++)
{
/* Match word with string */
found = 1;
for(j=0; j<searchLen; j++)
{
if(str[i + j] != toSearch[j])
{
found = 0;
break;
}
}
if(found == 1)
{
count++;
}
}
return count;
}
please check out your updated code... the added part and the updated part is mentioned... for any kind of doubt, please leave a comment....
as per your question, the program works properly... so, only the file part is added/modified i.e. only the file reading and writing part is added
code
#include <stdio.h>
#include <string.h>
// Maximum string size
#define MAX_SIZE 1000
int countOccurrences(char * str, char * toSearch);
int main()
{
char str[MAX_SIZE];
char toSearch[MAX_SIZE];
char ch;
int count,len,a[26]={0},p[MAX_SIZE]={0},temp;
int i,j;
FILE *fp1,*fp2; //ADDED
char fname[20]; //ADDED
//Take user's input
//printf("Enter any string : "); //******REMOVED********/
//___________ADDED__________
printf("\nEnter the source file :");
scanf("%s",fname);
fp1=fopen(fname,"r"); //OPENING FILE
i=0;
while(1){
ch = fgetc(fp1); //SCANNING CHARACTER BY CHARACTER
if(ch==EOF){
str[i]='\0';
break;
}
str[i]=ch; //STORING THE CHARACTER IN THE CHARACTER ARRAY
i++;
}
fclose(fp1); //READING COMPLETE
//____________________________________NEWLY ADDED________________
//gets(str); //__________________________NOT REQUIRED
//store length of string
len=strlen(str);
//array "a" contains count of every character
for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z')
a[str[i]-'a']++;
}
//output the result
//______________ADDED________________
printf("\nEnter the output file : ");
scanf("%s",fname);
fp2=fopen(fname,"w");
fprintf(fp2,"______________CHARACTER_OCCURANCE_____________________\n");
//______________-UPTO THIS__________
for(i=0;i<26;i++)
{
if(a[i]!=0)
{
ch=i +'a';
fprintf(fp2,"%c occurs %d times\n",ch,a[i]); //___________MODIFIED___________
}
}
//array "p" contains count of length of every word
fprintf(fp2,"________________COUNT OF EVERY WORD_________________\n"); //____________--ADDED_____________
for(i=0;i<len;i++)
{
temp=0;
while(i<len&&str[i]!=' ')
{
temp++;
i++;
}
p[temp]++;
}
//output the result
for(i=0;i<MAX_SIZE;i++)
{
if(p[i]!=0)
{
fprintf(fp2,"Word length %d occurs %d times\n",i,p[i]);//___________MODIFIED___________
}
}
fprintf(fp2,"\n_____________________WORD OCCURANCE_______________\n"); ////___________MODIFIED___________
for(i=0;i<len;i++)
{
memset ( toSearch, 0, MAX_SIZE );
temp=0;
while(i<len&&str[i]!=' ')
{
toSearch[temp]=str[i];
temp++;
i++;
}
count = countOccurrences(str, toSearch);
fprintf(fp2,"Total occurrences of '%s' is %d\n", toSearch, count); ////___________MODIFIED___________
}
fclose(fp2); //__________-ADDED_______________
return 0;
}
int countOccurrences(char * str, char * toSearch)
{
int i, j, found, count;
int stringLen, searchLen;
stringLen = strlen(str); // length of string
searchLen = strlen(toSearch); // length of word to be searched
count = 0;
for(i=0; i <= stringLen-searchLen; i++)
{
/* Match word with string */
found = 1;
for(j=0; j<searchLen; j++)
{
if(str[i + j] != toSearch[j])
{
found = 0;
break;
}
}
if(found == 1)
{
count++;
}
}
return count;
}