可以用一个计数器来记录字符串中出现的''的数量,如果计数器大于1,则说明字符串中不止一个''。

示例代码:

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

int main()
{
    char str[100];
    int count = 0;
    int i;

    printf("请输入字符串:");
    scanf("%s", str);

    for (i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == '*')
        {
            count++;
            if (count > 1)
            {
                printf("字符串中不止一个'*'
");
                return 0;
            }
        }
    }

    if (count == 1)
    {
        printf("字符串中只有一个'*'
");
    }
    else
    {
        printf("字符串中没有'*'或者不止一个'*'
");
    }

    return 0;
}
C语言判断字符串中是否只有一个'*'字符

原文地址: https://www.cveoy.top/t/topic/nf8N 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录