- C 编程有用的资源
- 按示例学习 C - 简要指南
- 按示例学习 C - 资源
- 按示例学习 C - 讨论
计算字符的字符串程序
实施
现在,我们将了解程序的实际实施 -
#include <stdio.h>
int main() {
char s[] = "TajMahal"; // String Given
char ch = 'a'; // Character to count
int i = 0;
int count = 0; // Counter
while(s[i] != '\0') {
if(s[i] == ch)
count++;
i++;
}
if(count > 0) {
if(count == 1)
printf("%c appears %d time in '%s'", ch, count, s);
else
printf("%c appears %d times in '%s'", ch, count, s);
} else
printf("%c did not appear in %s", ch, s);
return 0;
}
输出
此程序的输出应为 -
a appears 3 times in 'TajMahal'
string_programs_in_c.htm
广告