1. 写一个函数,查找下列字符串中a字符的个数,以及字符串中字符的总个数,
并返回给调用者.(不包含'\0'的字符个数和空格)
char str[] = "Wish you a happy Dragon Boat Festival";
错误答案:
unsigned long getCount(char* str,int* count)
{
int count = 0;
unsigned long len = strlen(str);
for(int i = 0;i < len;i++)
{
if(str[i] != '\0' && str[i] != ' ' )//当组字符等于空格或者是\0的时候,就跳出循环判断了
{
i--;
if (str[i] == 'a')
{
count++;
}
}
} 函数中注释掉的代码是有问题的
*count = aCount;
return len;
}
int main()
{
int count = 0;
unsigned long sum = getCount(str, &count);
printf("a的个数是%d,字符串的长度是%ld\n",count,sum);
return 0;
} |
|