【问题标题】:Vowels are not found correctly showing unusuall results未找到正确显示异常结果的元音
【发布时间】:2020-07-09 07:33:18
【问题描述】:

我正在尝试分别获取元音和辅音,并将它们从位于文件中的字符串保存到 "vowels.txt" 和 "consonant.txt" .但是辅音可以正确找到,但元音却没有。请看一下我的代码 sn-p 并帮助弄清楚我该如何解决这个问题。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int vowelIndex = 0;
int consIndex = 0;

int isVowel(char chr) {
    switch(chr) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return 1;
        default:
            return 0;
    }
}

char *readFile() {
    FILE* fp = fopen("character.txt","r");
    char str1[20];
    char* buffer = (char*)malloc(sizeof(char)*50);

//  fscanf(fp,"%s",str1);
    for (int i = 0; i < 6;i++) {
        fscanf(fp,"%s",str1);
        strcat(buffer,str1);
        strcat(buffer," ");
    }
    fclose(fp);
    return buffer;

}

void writeFile(char* fname, char* content) {
    FILE* fp = fopen(fname,"w"); // accessing the file in write mode
    fputs(content,fp);
    fclose(fp);
}

int main(void) {
    char *buff = readFile();
    char *vowels = (char*)malloc(sizeof(char)*18);
    char *cons = (char*)malloc(sizeof(char)*30);

    for (int i = 0; i < strlen(buff); i++) {
        if (isVowel(buff[i])) {
            printf("buffer inspect : %c\n",buff[i]);  
            vowels[vowelIndex] = buff[i];
            vowelIndex++;

        } else {
            cons[consIndex] = buff[i];
            consIndex++;
        }
    }

    printf("\nVowerls : %s\n",vowels);
    printf("\nCons : %s\n",cons);

    writeFile("vowels.txt",vowels); // writing to the file
    writeFile("consonant.txt",cons); // writing to the file:


    return 0;
}

请帮我找到这个。

【问题讨论】:

  • 当您发现您的程序无法运行时,您接下来会做什么?您是否通过在调试器中跟踪代码和/或添加调试打印语句来进行任何调试?如果有,你发现了什么?
  • 如果你想用puts来使用它们,你需要用\0来终止你的输出字符串。
  • strcat(buffer,str1); 不正确,因为buffer 内容未初始化,但strcat 要求第一个参数是字符串。使用buffer[0] = '\0'; 初始化
  • 你得到什么输出?对于哪个输入?你期望什么输出?你能描述一下区别吗?
  • 幻数有风险。

标签: c string file-handling


【解决方案1】:

您查找字符是元音还是字符的逻辑是正确的,但文件和空字符处理存在问题。

改变函数readFile如下:

char *readFile() {
    FILE* fp = fopen("character.txt","r");
    char str1[20];
    char* buffer = (char*)malloc(sizeof(char)*50);
int i;
  fgets(buffer,50,fp);
  /* fscanf(fp,"%s",buffer);
    for ( i = 0; i < 6;i++) {
        fscanf(fp,"%s",str1);
        strcat(buffer,str1);
        strcat(buffer," ");
    }
  */  fclose(fp);
    return buffer;

}

在main中,在for循环之后,在元音和cons字符串的末尾添加空字符,如下所示。

int main(void) {
    char *buff = readFile();
    char *vowels = (char*)malloc(sizeof(char)*18);
    char *cons = (char*)malloc(sizeof(char)*30);
    int i;
    printf("strlen = %d\n",strlen(buff));
    for (i = 0; i < strlen(buff); i++) {
        if (isVowel(buff[i])) {
            printf("buffer inspect : %c\n",buff[i]);
            vowels[vowelIndex] = buff[i];
            vowelIndex++;

        } else {
            cons[consIndex] = buff[i];
            consIndex++;
        }
    }
    vowels[vowelIndex] = '\0';
    cons[consIndex] = '\0';

    printf("\nVowels : %s\n",vowels);
    printf("\nCons : %s\n",cons);

    writeFile("vowels.txt",vowels); // writing to the file
    writeFile("consonant.txt",cons); // writing to the file:


    return 0;
}

【讨论】:

  • 非常感谢兄弟。最后我得到了解决方案。 :)
猜你喜欢
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 2022-05-11
相关资源
最近更新 更多