C语言程序代码解析 (尤其fun函数)

#include <conio.h>

#include <stdio.h> void fun (char *s,int a, int b) //a应为a,b应为b { while(*s) { if (*s>='A' && *s<='Z') (*a)++; //(*a)++; if (*s>='a' && *s<='z') (*b)++; //(*b)++; s++; } } int main() { char s[100];int upper=0,lower=0; clrscr(); // printf("请输入字符串:"); gets(s); fun(s,&upper, &lower); printf("n upper=%d lower=%d\n",upper,lower); }

统计大小写字母,fun函数是说如果字符数组s不为空就一直统计,大写字符a++,小写字符b++

统计大小写字符个数,代码是错误的,修改如下:

#include <conio.h>
#include <stdio.h> 
void fun (char *s,int *a, int *b)
{ 
  while(*s) 
  { 
    if (*s>='A' && *s<='Z') 
      (*a)++; 
    if (*s>='a' && *s<='z') 
      (*b)++; 
     s++; 
  } 
} 
int main() 
{ 
  char s[100];
  int upper=0,lower=0; 
  clrscr(); 
  printf("请输入字符串:"); 
  gets(s); 
  fun(s,&upper, &lower); 
  printf("upper=%d lower=%d\n",upper,lower); 
}

解释如下:

#include <conio.h>

#include <stdio.h> 
void fun (char *s,int* a, int *b) //a应为a,b应为b 
{ 
    while(*s) 
    { 
        if (*s>='A' && *s<='Z')  //如果字符是大写字符,个数+1
            (*a)++; //(*a)++; 
        if (*s>='a' && *s<='z') //如果字符是小写字符,个数+1
            (*b)++; //(*b)++; 
        s++; //字符指针后移,相当于*s 等价于s[i],s++等价于i++,s[i];
    } 
} 
int main() 
{ 
    char s[100];
    int upper=0,lower=0; 
    clrscr(); // 
    printf("请输入字符串:"); 
    gets(s); //从键盘输入字符串
    fun(s,&upper, &lower); //调用函数,获取大写字母和小写字母的个数
    printf("n upper=%d lower=%d\n",upper,lower);//屏幕输出大写和小写字母的个数
    return 0;
}