用c语言写一段代码 统计英文文本中长度大于等于3的单词
时间: 2024-05-09 20:18:56 浏览: 16
```
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
int main() {
char str[MAX_LEN];
int len, count = 0;
char *token;
printf("Please enter the text:\n");
fgets(str, MAX_LEN, stdin);
token = strtok(str, " \n");
while (token != NULL) {
len = strlen(token);
if (len >= 3) {
count++;
}
token = strtok(NULL, " \n");
}
printf("There are %d words with length greater than or equal to 3.\n", count);
return 0;
}
```
相关问题
用C语言写一段代码 判断英文文段中大于等于3个字母的单词
#include <stdio.h>
#include <ctype.h>
int main() {
char text[1000];
printf("请输入英文文段:\n");
fgets(text, 1000, stdin);
int count = 0;
int flag = 0;
for (int i = 0; text[i] != '\0'; i++) {
if (isalpha(text[i])) { // 如果是字母
if (!flag) { // 如果是单词的第一个字母
flag = 1; // 标记单词开始
}
} else { // 如果不是字母
if (flag) { // 如果上一个字符是单词的最后一个字母
count++; // 计数器加1
flag = 0; // 标记单词结束
}
}
}
printf("大于等于3个字母的单词个数为:%d\n", count);
return 0;
}
使用c语言来统计英文文本文件中单词个数写一串代码
这是一段 C 语言代码,可以用来统计英文文本文件中单词的个数:
```c
#include <stdio.h>
#include <ctype.h>
int main()
{
FILE* fp = fopen("input.txt", "r");
if (fp == NULL)
{
printf("Failed to open input.txt!");
return 1;
}
int wordCount = 0;
int inWord = 0;
char c;
while ((c = fgetc(fp)) != EOF)
{
if (isspace(c))
{
if (inWord)
{
wordCount++;
inWord = 0;
}
}
else
{
inWord = 1;
}
}
/* Count the last word, if any */
if (inWord)
{
wordCount++;
}
printf("Number of words: %d\n", wordCount);
fclose(fp);
return 0;
}
```
注:该代码将输入的文件名定为 input.txt,可以根据实际情况进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)