首页 > 解决方案 > c 中的 fgets() 根本不读句子?

问题描述

我对C非常陌生。现在尝试练习输入和输出。我正在尝试使用 fgets 阅读一个句子并将其转换为所有大写字母,但由于某种原因它没有阅读它。我在我的 SCANF 中添加了 *c,但它仍然不起作用。非常感谢任何建议和提示!提前致谢。

  #include <stdio.h>
#include <ctype.h>

#define ARR_SIZE 100

void modify(int pInt[100], int input);

void modify2(char *pString[100]);

int main() {
    printf("Enter a size for your array:\n");
    int input;
    scanf("%d", &input);
    printf("Great! You now have an array of size %d.\n", input);
    int array[ARR_SIZE];
    printf("Now please enter %d numbers.\n", input);
    int i;
    for (i = 0; i < input; i++) {
        scanf("%d*c", &array[i]);
    }
    printf("Awesome. All %d slots in the array are now filled.\n", input);
    printf("Here is how your array looks:\n");
    int j;
    for (j = 0; j < input; j++) {
        printf(" %d |", array[j]);
    }
    printf("\n");
    modify(array, input);
    printf("Now your array looks like this:\n");
    for (j = 0; j < input; j++) {
        printf(" %d |", array[j]);
    }
    printf("\n");

    printf("Experiment #2:\n");
    printf("Enter a sentence:\n");
    char sentence[ARR_SIZE];
    fgets(sentence, ARR_SIZE, stdin);
    modify2(sentence);
    printf("Now your sentence is upper case: %s\n", sentence);
    return 0;
}

void modify2(char *pString[100]) {
    int i = 0;
    while (pString[i]) {
        pString[i] = toupper(pString[i]);
        i++;
    }
}

void modify(int pInt[100], int input) {
    int i;
    for (i = 0; i < input; i++) {
        pInt[i] *= 2;
    }
}

标签: cfgets

解决方案


推荐阅读