C 从文本文件中查找不包含元音的单词
大家好,我是c编程语言的新手,在编写一些代码时遇到了一些问题。我的任务是在一个包含数千个单词的文本文件中循环,找到不包含元音的单词。我已经能够阅读文本文件并打印出所有的单词,但是没有找到没有元音的单词。当我运行我的代码时,我要么得到所有的单词,要么没有输出,我不知道为什么。如果我能得到任何帮助,我将不胜感激C 从文本文件中查找不包含元音的单词,c,C,大家好,我是c编程语言的新手,在编写一些代码时遇到了一些问题。我的任务是在一个包含数千个单词的文本文件中循环,找到不包含元音的单词。我已经能够阅读文本文件并打印出所有的单词,但是没有找到没有元音的单词。当我运行我的代码时,我要么得到所有的单词,要么没有输出,我不知道为什么。如果我能得到任何帮助,我将不胜感激 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LENGT
#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`
#包括
#包括
#包括
#定义最大长度64
int main()
{
文件*f=fopen(“test.txt”、“r”);
字符字[最大长度];
int长度=strlen(字);
int i;
while(fgets(字,最大长度,f)){
对于(i=0;i您需要在while和for之间移动行长=strlen(word)。您需要在while和for之间移动行长=strlen(word)。包括
#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,j,k,flag=0;
fgets(word, MAX_LENGTH, f);
for (i = 0; i<length; i++)
{
if(word[i]==" ")
{
flag=0;
j=i;
for(k=i;word[k]!=" ";k++)
{
if(word[i] == 'a' | word[i] == 'e'|
word[i] == 'i' | word[i] == 'o'|
word[i] == 'u' | word[i] == 'y')
{
flag=1;
break;
}//end of if statement
}
if(flag==0)
for(i=i;i<=k;i++) printf("%c",word[i]);
}
}//end of for loop
}
#包括
#包括
#定义最大长度64
int main()
{
文件*f=fopen(“test.txt”、“r”);
字符字[最大长度];
int长度=strlen(字);
int i,j,k,flag=0;
fgets(字,最大长度,f);
对于(i=0;i#包括
#包括
#包括
#定义最大长度64
int main()
{
文件*f=fopen(“test.txt”、“r”);
字符字[最大长度];
int长度=strlen(字);
int i,j,k,flag=0;
fgets(字,最大长度,f);
对于(i=0;i此代码采用不同的方法。它将使用fgets
读取每一行,然后使用sscanf
查找字符串中的每个单词。这应该处理单词之间的制表符和空格。如果行太长,它还可以确保不会溢出输入缓冲区。如果行太长,则fgets
将填充用它找到的字符串缓冲区。我们必须确保跳过所有剩余字符直到换行符\n
,以便正确找到下一行的开头
代码还修复了一些问题。字符串长度在原始海报(OP)中设置代码位于错误的位置。按位的|
已更改为逻辑或|
,代码已修改为搜索单词中的元音,如果找到元音,则设置标志。如果单词不包含元音,则打印出来。代码应在C89编译器(或更高版本)下工作
可以找到有关使用sscanf
解析字符串的信息
此代码采用不同的方法。它将使用fgets
读取每一行,然后使用sscanf
查找字符串中的每个单词。这应该处理单词之间的制表符和空格。它还确保如果一行太长,我们不会溢出输入缓冲区。如果一行太长,则fgets
将填充字符串缓冲区。我们必须确保跳过所有剩余字符,直到换行符\n
,以便正确找到下一行的开头
代码还修复了一些问题。字符串长度在原始海报(OP)中设置代码位于错误的位置。按位的|
已更改为逻辑或|
,代码已修改为搜索单词中的元音,如果找到元音,则设置标志。如果单词不包含元音,则打印出来。代码应在C89编译器(或更高版本)下工作
可以找到有关使用sscanf
解析字符串的信息
我想,测试一个单词是否包含元音的逻辑是不正确的。文本文件格式是什么?每行包含一个单词或类似于用空格分隔的一行中的所有单词?单词是不能全部打印的总字符串。好的,我带着我的答案来了。|
而不是|
具有短路e的优点顺便说一句,除了穆罕默德和马夫索的评论
需要在内部而不是外部。我想测试一个单词是否包含元音的逻辑是不正确的。文本文件格式是什么?每行包含一个单词或类似于用空格分隔的一行中的所有单词?word是不能全部打印的总字符串。好的,我带着我的答案来了。|
而不是<代码>
具有短路评估的优点,顺便说一句……除了穆罕默德和马夫索的评论length=strlen(word);
需要在内部而不是外部。这只是众多问题中的一个。你需要这样做,你应该将位
改为|
(为了效率)您需要通过fgets
从文件中读取单个单词而不是完整字符串,或者如果您使用fgets
,则需要在处理之前将生成的字符串分解为组成单词。这只是众多问题中的一个。您需要这样做,您应该将按位
更改为|
(为了提高效率)您需要通过fgets
从文件中读取单个单词而非完整字符串,或者如果您使用fgets
,则需要在处理前将生成的字符串分解为组成词。您了解masfo对问题的评论吗?谢谢,但我在此处找到但没有看到masfo的评论@James Morris您了解masfo的评论吗对这个问题有何评论?谢谢,但我在这里找到了但没有看到Masfo的评论@James Morrish谢谢你提供了到strtok和sscanf的链接。作为c的新手,我完全不知道这些功能。在阅读了你的更正和补充后,我理解得更清楚了。谢谢你的帮助。我看到你将其删除为已接受的回复。是吗有一个问题我可以纠正吗?我还看到,在我发布了这个答案之后,你实际上修改了你的问题(在后续评论中),说明了一些新的东西——每个单词都在一行上。这是你的问题中遗漏的。事实上,如果是这样的话,有一个比我发布的答案简单得多的解决方案
#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 */
#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 */