题目:分别统计字符串中大写字母和小写字母的个数。
代码如下:
#include <conio.h> #include <stdio.h> void fun(char *s, int *a, int *b)//定义一个字符指针指向实参s,两个整形指针指向实参upper和lower { while (*s)//遍历传递过来的字符串 { if (*s >= 'A' && *s <= 'Z') (*a)++;//注意是先取值,地址再加一 if (*s >= 'a' && *s <= 'z') (*b)++; s++; } } main() { char s[100]; int upper = 0, lower = 0; printf("\nPlease a string : "); gets(s); fun(s, &upper, &lower);//实现地址传递 printf("\n upper = %d lower = %d\n", upper, lower); }
输出结果如下: