#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char ar[] = {"i am a student"};//定义字符数组
int len = sizeof(ar) / sizeof(ar[0]);//获取字符数组ar的元素个数
int count = 0;
for(int i = 0;i <= len; ++i){//开启循环,当循环至字符数组中的‘a’时,count+1
if(ar[i] == 'a'){
count++;
}
}
printf("该字符串的长度为:%d\n",len);//输出字符数组的长度
printf("该字符串中字符a的个数为:%d\n",count);//输出字符数组中‘a’的个数
return 0;
}
如图为程序运行结果:
输出完成