大家好,我是 c 编程语言的新手,我在处理一些代码时遇到了一些麻烦。我的任务是遍历包含数千个单词的文本文件并找到不包含元音的单词。我已经能够阅读文本文件并打印出所有单词,但未能成功找到没有元音的单词。当我运行我的代码时,我要么得到所有的单词,要么没有输出,我不确定为什么。我将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 64
int main()
{
FILE* f = fopen("test.txt", "r");
char word[MAX_LENGTH];
int length = strlen(word);
int i;
while (fgets(word, MAX_LENGTH, f)) {
for (i = 0; i<length; i++){
if(word[i] == 'a' | word[i] == 'e'|
word[i] == 'i' | word[i] == 'o'|
word[i] == 'u' | word[i] == 'y'){
break;
}//end of if statement
else{
printf("%s ", word);
}//end of else
}//end of for loop
}//end of while loop
}//end of main`
最佳答案
这段代码采用了不同的方法。它将使用 fgets
读取每一行,然后使用 sscanf
查找字符串中的每个单词。这应该处理单词之间的制表符和空格。如果一行太长,它还确保我们不会溢出输入缓冲区。如果一行太长,fgets
将用找到的内容填充字符串缓冲区。我们必须确保跳过所有剩余字符直到换行符 \n
以便我们能够正确找到下一行的开头。
该代码还修复了一些问题。字符串长度在错误位置的原始发布者 (OP) 代码中设置。按位 |
已更改为逻辑或 ||
并且代码已修改为搜索单词中的元音并在找到时设置标志。如果一个单词不包含元音,它就会被打印出来。该代码应在 C89 编译器(或更高版本)下运行。
关于使用sscanf
解析字符串的信息可以找到here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 512
#define MAX_WORD_LENGTH MAX_LINE_LENGTH
int
main()
{
FILE *f = fopen("test.txt", "r");
char line[MAX_LINE_LENGTH];
/* Process each line in the file */
while (fgets(line, MAX_LINE_LENGTH, f)) {
char word[MAX_WORD_LENGTH];
char *curline = line; /* current pointer in the string we are processing */
int charsread;
/* If we read maximum number of characters then make
* sure we flush the rest of the line up until the newline so
* the next pass will start at the beginning of the next line */
if ((line[MAX_LINE_LENGTH - 1] != '\0')
&& (line[MAX_LINE_LENGTH - 1] != '\n')) {
while (fgetc(f) != '\n');
}
/* Scan for each word in the string we read */
while (sscanf(curline, "%s%n", word, &charsread) > 0) {
int i;
int wordlen = strlen(word);
int vowelfnd = 0; /* vowelfnd set as false */
for (i = 0; i < wordlen; i++) {
if (word[i] == 'a' || word[i] == 'e' ||
word[i] == 'i' || word[i] == 'o' ||
word[i] == 'u' || word[i] == 'y') {
/* Set vowelfnd to true */
vowelfnd = 1;
break;
} /* end of else */
} /* end of for loop */
/* If we didn't find a vowel print word */
if (!vowelfnd)
printf("%s ", word);
/* Advance past the word we just read in the
* string buffer */
curline += (charsread);
}
} /* end of while loop */
return 0;
} /* end of main */
从字符串中读取单词的一种更高级的方法(我会使用的方法)是使用 strtok
C 函数,您可以找到更多关于 here 的信息.该链接还包含一些示例代码,可用于搜索字符串中的单词。 strtok 的一个优点是您可以轻松指定其他单词分隔符,如句点、问号等。
在 OP 发布问题后,他们添加了一条额外的信息。提供的所有解决方案都假定一行中有多个单词,但事实并非如此。那么最简单的解决方案就是修复原始版本中的错误:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 64
int
main()
{
FILE *f = fopen("test.txt", "r");
char word[MAX_LENGTH];
int i;
int length;
while (fgets(word, MAX_LENGTH, f)) {
int vowelfnd = 0; /* Vowel found false */
/* Remove the end of line character(s) */
strtok(word, "\n");
length = strlen(word);
for (i = 0; i < length; i++) {
if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' ||
word[i] == 'o' || word[i] == 'u' || word[i] == 'y') {
vowelfnd = 1; /* Vowel found true */
break;
} /* end of if statement */
} /* end of for loop */
if (vowelfnd)
printf("%s ", word);
} /* end of while loop */
return 0;
} /* end of main */
关于c - 从文本文件中查找不包含元音的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836642/