#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_FILENAME_LEN 256 #define MAX_LINE_LEN 1024 struct CountResult { int word_count; int letter_count; int digit_count; int space_count; int newline_count; int punct_count; int other_count; }; struct CountResult count_chars(const char* filename) { struct CountResult result = { 0, 0, 0, 0, 0, 0, 0 }; FILE* fp; char line[MAX_LINE_LEN]; int c; int i; fopen(filename,"r"); if (fp == NULL) { fprintf(stderr, "Error: cannot open file '%s'\n", filename); exit(1); } while (fgets(line, MAX_LINE_LEN, fp) != NULL) { for (i = 0; line[i] != '\0'; i++) { c = line[i]; if (isalpha(c)) { result.letter_count++; } else if (isdigit(c)) { result.digit_count++; } else if (isspace(c)) { result.space_count++; if (c == '\n') { result.newline_count++; } } else if (ispunct(c)) { result.punct_count++; } else { result.other_count++; } } char* p = line; while (*p != '\0') { while (*p != '\0' && !isalnum(*p)) { p++; } if (*p != '\0') { result.word_count++; while (*p != '\0' && isalnum(*p)) { p++; } } } } fclose(fp); return result; }
时间: 2024-02-10 09:19:28 浏览: 30
这段代码是一个 C 语言程序,用于统计一个文件中的字符、单词、数字、空格、换行符、标点符号和其他字符的数量。函数 `count_chars` 接受一个文件名作为参数,返回一个结构体 `CountResult`,包含上述统计结果。
程序首先定义了结构体 `CountResult`,其中包含了各种要统计的数量。然后定义了一个函数 `count_chars`,该函数的参数是一个字符串,表示要统计的文件名。函数内部定义了一个结构体 `result`,用于保存统计结果。
函数首先尝试以只读方式打开文件,若打开失败则报错并退出程序。然后使用 `fgets` 函数逐行读取文件内容,同时使用循环遍历每个字符,并根据其类型进行统计。
统计字母、数字、空格、换行符和标点符号的数量比较简单,只需要使用 C 标准库中的 `isalpha`、`isdigit`、`isspace`、`ispunct` 函数判断字符类型即可。统计其他字符的数量则可以使用 `else` 分支。
统计单词数量的方法比较巧妙,使用了两个嵌套的 `while` 循环。首先将字符指针 `p` 指向当前行的开头,然后在循环中不断向后移动 `p`,直到找到一个字母或数字字符。此时说明找到了一个单词,将单词计数器加一,并继续向后移动 `p` 直到找到下一个非字母或数字字符。这样就可以统计出该行中所有的单词数量。
最后,函数关闭文件,并将统计结果结构体返回。
相关问题
C++编制一个读单词过程,以#include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #define NULL 0为开头
C++编写一个读取单词的过程可以以下代码:
```cpp
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
void readWord(char* word) {
int index = 0;
char ch;
// 跳过空白字符
while ((ch = getchar()) != EOF && isspace(ch));
// 读取单词字符
while (ch != EOF && !isspace(ch)) {
word[index++] = ch;
ch = getchar();
}
// 添加字符串结束符
word[index] = '\0';
}
int main() {
char word[100];
readWord(word);
printf("读取的单词是:%s\n", word);
return 0;
}
```
上述代码中,`readWord`函数用于读取一个单词,它会跳过输入中的空白字符,然后读取连续的非空白字符作为一个单词。`main`函数中调用了`readWord`函数,并打印出读取到的单词。
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_FILENAME_LEN 256 #define MAX_LINE_LEN 1024 struct CountResult { int word_count; int letter_count; int digit_count; int space_count; int newline_count; int punct_count; int other_count; }; struct CountResult count_chars(const char* filename) { struct CountResult result = { 0, 0, 0, 0, 0, 0, 0 }; FILE* fp; char line[MAX_LINE_LEN]; int c; int i;
这段代码定义了一个名为 CountResult 的结构体,包含了单词数量、字母数量、数字数量、空格数量、换行数量、标点符号数量和其他字符数量这七个成员变量。然后定义了一个名为 count_chars 的函数,该函数接受一个文件名作为参数,返回一个 CountResult 结构体类型的结果。
在函数内部,首先定义了一个名为 result 的 CountResult 结构体变量,并将其所有成员变量初始化为 0。然后打开了该文件名对应的文件,并逐行读取文件内容。在每一行中,函数统计了字母、数字、空格、换行、标点符号和其他字符的数量,并将结果累加到 result 结构体变量的相应成员变量中。
最后,函数返回了统计结果 result。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)