2116 - 大写字母统计

 写一个函数,找出给定字符串中大写字母字符(即'A'-'Z'这26个字母)的个数(如字符串”China Computer Wrold”中大写字母字符的个数为3个)。要求用子函数和Driver的形式编写代码。

input:

helloABCworld
output:
3

题目输入

字符串,可能包含空格

题目输出

大写字母的个数

输入/输出样例

题目输入

hello ABC world

题目输出

3

C语言解答

#include <stdio.h>

int k;
char c;

int main(void)
{
    scanf("%c",&c);
    while ( c != '\n' )
    {
        if ( c > 'A' - 1 && c < 'Z' + 1 )   k++;
        scanf("%c",&c);
    }
    printf("%d\n",k);
    return 0;
}

C++解答

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
void str(string);
int main()
{
	string line;
	getline(cin,line);
	str(line);
	return 0;
}
void str(string a)
{
	int n,i;
	n=0;
	for(i=0;i<a.length();i++)
	{
		if(isupper(a[i]))
			n++;
	}
	cout<<n<<endl;
}
    
时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题