求字符数目,输入字符串时显示Segmentation fault (core dumped)

int main(void)
{
    char a[100];

    printf("please input char:\n");
    scanf("%s", &a);
    f(a);

    return 0;
}

void f(char *a)
{
    int i = 0;
    int space = 0, line = 0, others = 0;

    while(a[i] != '#')
    {
        switch(a[i])
        {
            case ' ': space++; break;

            case '\n': line++; break;

            default : others++; break;  

        }

        i++;
    }
        printf("space count is:%d\n", space);
        printf("line count is:%d\n", line);
        printf("other count is:%d\n", others);

}

求字符数目,输入字符串时显示Segmentation fault (core dumped),请问是什么原因?应该怎么改?

scanf("%s",a);

while(a[i] != '#')
最好写
while(a[i] != '\0')
不然你忘记输#程序会越界。

scanf("%s", &a);
->
scanf("%s", a);
或者
scanf("%s", &a[0]);