C语言判断字符串中是否只出现一次'*'符号
在C语言字符串中,只允许出现一次'*',可以使用以下代码进行判断:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 0;
printf('请输入字符串:');
scanf('%s', str);
for (int i = 0; i < strlen(str); i++) {
if (str[i] == '*') {
count++;
}
}
if (count == 1) {
printf('字符串中只出现了一次*\n');
} else {
printf('字符串中出现了多次*或没有出现*\n');
}
return 0;
}
在上述代码中,使用了一个计数器变量count来记录字符串中出现''的次数,如果count等于1,则表示字符串中只出现了一次''。
原文地址: https://www.cveoy.top/t/topic/mSIf 著作权归作者所有。请勿转载和采集!