Question

In: Computer Science

Modify this program so that it takes in input from a TEXT FILE and outputs the...

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;

}

Solutions

Expert Solution

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;
}

Related Solutions

Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Write a program that takes a date as input and outputs the date's season. The input...
Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June 20 Summer:...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a...
IN PYTHON Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string....
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
Write a program in python that takes a date as input and outputs the date's season....
Write a program in python that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. Ex: If the input is: April 11 the output is: Spring In addition, check if the string and int are valid (an actual month and day). Ex: If the input is: Blue 65 the output is: Invalid The dates for each season are: Spring: March 20 - June...
Write the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT