繁体   English   中英

从文本文件中查找不包含元音的单词

[英]finding words that contain no vowels from a text file

祝一切顺利,我对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解析字符串的信息,请参见此处

#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函数,您可以在此处找到更多信息。 该链接还包含一些示例代码,可用于搜索字符串中的单词。 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 */
#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 
}

您需要在while和for之间移动line length = strlen(word)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM