-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountWord.c
More file actions
61 lines (58 loc) · 1.04 KB
/
Copy pathCountWord.c
File metadata and controls
61 lines (58 loc) · 1.04 KB
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
#include<stdio.h>
#include<string.h>
#include<locale.h>
#define M 256
int WordCount(char* buf);
int main()
{
setlocale(LC_ALL, "Russian");
char buf[M] = { 0 };
int result = 0;
printf("Enter the string!\n");
fgets(buf, M, stdin);
if (buf[strlen(buf) - 1] == '\0')
buf[strlen(buf) - 1] = 0;
result = WordCount(buf);
printf("Количество слов в строке %d", result);
return 0;
}
int WordCount(char* buf)
{
int count = 0, result = 0,flag = 0;
int size = strlen(buf);
int i,j,k;
for (i = 0; i < size; i++)
{
if (buf[i] != ' ' && buf[i + 1] != ' ' && flag == 0)
{
flag = 1;
count++;
}
}
for(j = 0; j< size; j++)
{
if (buf[j] == ' ' && buf[j + 1] != ' ' && flag == 0)
{
flag = 1;
count++;
}
else if (buf[j] != ' ' && buf[j + 1] != ' ' && flag == 1)
{
flag = 0;
}
}
for (k = 0; k < size; k++)
{
if (buf[k] != ' ' && buf[k + 1] != ' ' && flag == 0)
{
flag = 1;
count++;
}
if (buf[k] == '\0' || buf[k + 1] == '\n' || flag ==1)
{
flag = 0;
count--;
}
}
return count;
}