Strings, String Processing and fileIO

Hi, this is my first time here in this website I'm just curious if someone will reply with an answer using c code. Thanks in advance if ever you reply
Write a program that will satisfy the following:

The program can read a text file named input.txt. This file contains the text of a news article consisting of at least 100 words.
The program asks the user to input a word.
The program reads the contents of the text file and counts how many times the inputted word appears. The program also determines how sentences there are in the file. Assume that every sentence is terminated by a period.
the main() function should only contain variable declarations and functions calls. This means that you have to implement program functionality using user-defined functions.
This seems like homework? What problems are you having? How much have you already done? What don't you understand or can't do? Post what you have already so that we can advise. Have you produced a design for the program?
c or c++?

Ill give you a hint or two.
c would use FILE* to open a file, choosing one of fread, fgetc, fgets, or fscanf to read it.
Then you probably want to iterate it, convert to upper or lower case (so you can find the word no matter what case it used, eg The vs the vs THE are all different bytes and compare differently) and count the periods (just return int for period count and case it in the internal loop). (yes, the function does 2 things, but looping the data twice is senseless). Then its just breaking it up by words, strtok may help here, or just use space character and DIY.
the whole program is probably 10-15 lines not counting user prompts, comments, includes, or function define lines (that is, the actual do stuff lines are 10-15 ish is all you are going to need). If you find yourself with more than twice that, you are trying too hard / thinking about it too linearly.
Last edited on
-seeplus
-jonnin
Hi guys sorry for the late reply, I never thought someone will reply to me. Im done with that program already. Im just testing if someone will reply:)

This is my code

#include <stdio.h>
#include <string.h>
int file()
{
FILE *fp;
char Size[100];

fp = fopen("//TITLE OF THE TEXT.txt//", "r");
printf("//TITLE OF THE ARTICLE//");
printf("\n" );
printf("\n" );

if (fp == NULL)
{
printf("Error" );
return 0;
}

while(fscanf(fp, "%s", Size)!=EOF){
printf("%s ", Size );
}
fclose(fp);
}
int SENTENCECOUNT()

{

FILE *fp;
int count = 0;

char sen;

fp = fopen("//TITLE OF THE TEXT.txt//", "r");
if (fp == NULL)
{
printf("Error" );
return 0;
}

for (sen = fgetc(fp); sen != EOF; sen = getc(fp))
if (sen == '.')
{
count = count + 1; }
fclose(fp);
printf("\The document has %d sentences.\n ", count);
return 0;

}
int CountWord()
{
FILE *fp;
fp = fopen("//TITLE OF THE TEXT.txt//", "r");
if (fp == NULL)
{
printf("Error" );
return 0;
}
char str[1000];
char *pos;
int ind, count;
count = 0;
char word[50];
printf("Enter the word you want to look in the document: ");
scanf("%s", word);
while ((fgets(str, 1000, fp)) != NULL)
{
ind = 0;
while ((pos = strstr(str + ind, word)) != NULL)
{
ind = (pos - str) + 1;
count++;
}
}

printf("'%s' is found %d times in document.", word, count);
return 0;
}
int main()
{

file();
printf("\n");
printf("\n");
printf("Number of sentences are:\n");
SENTENCECOUNT();
printf("\n");
printf("\n");
printf("What word do you want to look for?\n");
printf("\n");
CountWord();

return 0;
}
Topic archived. No new replies allowed.