Counting Word

a program to count the number of
occurrences of words in a passage.
The program will read white-space separated
words from a file, convert all words to a
single case, and remove any non-alphanumeric
characters from both ends of the words. It is
to count the number of each word, and
then write to a file a list of word/count pairs, sorted by word.

I am stuck at read from file and store it to var.
it work with user input.
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
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[200];
void main()
{
	void opx(char *);
	char str1[20];
	int x = 0, y = 0, i;
	FILE *inputf;
	inputf = fopen("text.txt", "r");
	if (inputf == NULL)
	{
		printf("cant open it")
	}
	while (inputf != eof)
	{
		fscanf(inputf, "%s");
	}

	/*printf("enter the string");
	gets(str);
	while (str[y] != 0 && y<strlen(str))
	{
		for (i = 0; str[i] != '\0'; i++)
		{
			str1[i] = '\0';
		}
		while (str[y] != ' '&& y<strlen(str) && str[y] != '\0')
		{
			str1[x] = str[y];
			x++;
			y++;
		}
		if (str1[0] != '\0')
		{
			opx(str1);
		}
		x = 0;
		y++;
	}
	getch();*/
}
void opx(char *word)
{
	char s;
	size_t i, j, count, c, h;
	i = j = 0;
	count = 0;
	c = strlen(word);
	while (str[i] != '\0' && i<strlen(str))
	{
		h = i;
		while (str[i] != ' '&& i<strlen(str) && str[i] != '?'&& str[i] != '.'&& str[i] != ',')
		{
			if (j == c && str[i] != ' '&& str[i] != '?' && str[i] != '.' && str[i] != ',')
			{
				j++;
			}
			if (str[i] == word[j] && str[i] != ' '&& str[i] != '?' && str[i] != '.' && str[i] != ',')
				j++;
			i++;
		}
		if (c == j)
		{
			count++;
			while (str[h] != ' ' && str[h] != '\0')
			{
				if (str[h] == '\0')
				{
					str[h] = '\0';
				}
				else
					str[h] = ' ';
				h++;
			}
		}
		j = 0;
		i++;
	}
	printf("\n%s word occurs %d times", word, count);
}
Topic archived. No new replies allowed.