表示字符串中某个字符的数量

img


想表示字符串中'{'的数量要怎么表示?
char表示字符串怎么表示?char a[]和char*a的区别是什么


#include<stdio.h>
#include<string.h>
int main()
{
    char str[50] = { 0 };
    scanf("%s", str);//数组名是首元素的地址,所以不要&
    int count = 0;
    int len = strlen(str);//求字符串的长度
    int i = 0;
    for (i = 0; i < len; i++)//遍历字符串
    {
        if (str[i] =='{')
        {
            count++;
        }
    }
    printf("%d ", count);
    return 0;
}

img

注意‘{’不是双引号,这里是字符,它的值对应的是ascii码表里对应的值123,所以应该当a[123]为大于0的值时,一直会进行while循环。
至于char的区别,看这里:https://blog.csdn.net/snowsnowsnow1991/article/details/78043598

题主代码修改如下,供参考:

#include <stdio.h>
int main()
{
    char a[256];
    int  i = 0, n = 0;
    scanf("%s", a);
    while (a[i] != '\0') {
        if (a[i] == '{') n++;
        i++;
    }
    printf("%d", n);
    return 0;
}