从用户输入获取字符串值并将每个字母保存到数组中

时间:2020-02-12 07:58:18

标签: c

我想从用户那里获取字符串值并将其保存到数组中。例如,如果用户打招呼,我想将'h'保存到array [0],将'e'保存到array [1],依此类推... 这是我的代码,但出现错误(分段错误核心转储)。我不知道如何解决它,请帮助我。

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


int main(){
    char array[50];
    int i=0;

    printf("Write any word you want without space:");
    while(array[i]!='\0'){
        scanf("%s",array[i]);
        i++;
    }
    for(int j=0;j<=i;j++){
        printf("%s",array[j]);
    }
return 0;
}

1 个答案:

答案 0 :(得分:1)

您正在做的是C自动执行的操作。 试试这个:

int main(){
    char array[50];
    int i=0;

    printf("Write any word you want without space:");
    scanf("%s",array);
    printf("%s",array);
    return 0;
}