Counting the word

I dont know if this a right place to ask but anyway here you go:

I am trying count all occurrences word are that are in the file but i dont know how to do it but I can only do in like user have input in not from a file;

EX:

in file they have

the 50
is 20
on 5

and so on. but i couldnt get it to work.
I hope someone could help me out.

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
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
	int i = 0, j = 0, count = 0;
	char str1[100], str2[20], str3[20];
	//clrscr();
	printf("Enter the text: ");
	gets(str1);

	printf("Enter word to count: ");
	gets(str2);

	while (str1[i] != '\0')
	{
		while (str1[i] != ' '&&str1[i] != '\0')	//copying the word from the text to a new string
			str3[j++] = str1[i++];

		str3[j] = '\0';	//assigning null character at the end of string
		j = 0;

		if ((_strcmpi(str2, str3)) == 0)	//comparing the given word with the copied word
			count++;

		if (str1[i] == '\0')
			break;
		else
			i++;
	}

	printf("No. of words are %d", count);
	_getch();
}
When you say 'couldn't get it work', is that you can't read the file or the count is incorrect or anything else?
first on all i am new to C as you can see i ask user for input but thing is i want to get information file not from user;

like in file it store

50 the
20 so
10 on

it will print

50 the
20 so
10 on

and so on

code is working fine! I compile it and it run
Last edited on
i dont know to store information and count at same time. also dont how to get compiler to read the file
So you want to replace

1
2
printf("Enter the text: ");
gets(str1);


into something read whole content of a file?


yes. like i say...

it will store "the" ; "so" and "on" and count it how many time it appear on file. NOT only the so on but it could be like i you me they where were,
both
1
2
3
4
5
printf("Enter the text: ");
	gets(str1);

	printf("Enter word to count: ");
	gets(str2);
I'm going to provide you some functions to operate a file. But you'll have to combine them yourself.

To open a file for reading:
1
2
FILE *fp;
fp = fopen(filename, "rb");


To close a file:
fclose(fp);

To get the size of a file
1
2
3
long size;
fseek(fp, 0, SEEK_END);
size = ftell(fp);


To read the file into a string
1
2
3
4
5
char *buffer;
buffer = malloc(size + 1);
fseek(fp, 0, SEEK_SET);
fread(buffer, 1, size, fp);
buffer[size] = '\0';
if you read into a sting how can you compare the word?

like the and the?
i am confuse already
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
#include<conio.h>
#include<string.h>

void main()
{
	int i = 0, j = 0, count = 0;
	char str1[100], str2[20], str3[20];
	//clrscr();
	FILE *openfile;
	if ((openfile = fopen("text.txt", "r")) == NULL)
	{
		return -1;
	}
	gets(str1);

	printf("Enter word to count: ");
	gets(str2);

	while (str1[i] != '\0')
	{
		while (str1[i] != ' '&&str1[i] != '\0')	//copying the word from the text to a new string
			str3[j++] = str1[i++];

		str3[j] = '\0';	//assigning null character at the end of string
		j = 0;

		if ((_strcmpi(str2, str3)) == 0)	//comparing the given word with the copied word
			count++;

		if (str1[i] == '\0')
			break;
		else
			i++;
	}

	printf("No. of words are %d", count);
	_getch();
}
after that i dont get it
here is what i did in c++
....
Last edited on
Your first C program ASKS THE USER FOR A LINE, and them count the words.
Change that part to read the whole file. You don't have to change the rest of the code.
you mean while loop and the two cout?
First of all, write a short function to read whole content of a file

1
2
3
4
5
6
char* loadfile(const char *filename)
{
    char *buffer;
    // do it yourself
    return buffer;
}


then, replace

1
2
printf("Enter the text: ");
gets(str1);


with something like

str1 = loadfile("myfile");
Topic archived. No new replies allowed.