首页 文章

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

提问于
浏览
1

美好的一天,我是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`

3 回答

  • 2

    此代码采用不同的方法 . 它将使用 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 */
    
  • 3

    你需要在while和for之间移动行长度= strlen(word) .

  • 0
    #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 
    }
    

相关问题