当前位置:编程学习 > VC++ >>

用VC++编程2

给定程序的功能是分别统计字符串中大写字母和小写字母的个数。例如,给字符串ss输入:AaaaBBb123CCccccd,则输出结果应为:upper=5,lower=9。注意:部分源程序给出如下。请写出fun函数的内容。

试题程序:

#include <stdio.h>

void fun ( char *s, int *a, int *b )

{

}

main( )

{

char str[100];

int upper = 0, lower = 0 ;

printf("\n请输入一个字符串:" );

scanf("%s",str);

fun ( str, & upper, &lower );

printf( "\n upper = %d lower = %d\n", upper,lower );

}

答案:

void fun ( char *s, int *a, int *b )
{
while (*s != 0)//为读至字符串最后就读下去
{
if (*s >= 'a' && *s <= 'z')
*b += 1;
else if (*s >= 'A' && *s <= 'Z')
*a += 1;
s++;
}
}

void fun ( char *s, int *a, int *b )

{

*a = *b = 0;

while(*s) {

if(*s >= 'A' && *s <= 'Z') ++*a;

else if(*s >= 'a' && *s <= 'z') ++*b;

++s;

}

}

上一个:用VC++编程
下一个:用VC++编程3

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,