/**
* Copyright (C) 2014, CSU
* All rights reservedl
* File Name:test.cpp
* Author: lmm
* Date of completion: 2014/10/6
* Version: v1.0
*
* 问题描述:找出给定字符串中大写字符(即‘A’-‘Z’)的个数
* 接口说明:原型:int CalcCapital(string str) 返回值:int
* 输入描述: 输入一个string数据
* 样例输入:add123#$%#%#O
* 程序输出: 输出string中大写字母的个数,需考虑字符串为空 如果为空输出0
*/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int count = 0;
int CalcCapital(string str)
{
if(str.size() != 0)
{
for(string::size_type ix = 0; ix != str.size(); ++ix )
{
if(isupper(str[ix]))
++count;
}
return count;
}
else
return 0;
}
int main()
{
string s;
while(cin >> s)
count = CalcCapital(s);
cout << count << endl;
return 0;
}
总结:提交一次,状态显示错误,查找原因,系统判断时给出3个测试用例,其中一个估计是字符串中含有空格,导致的错误,应该用while循环将字符串放入流中,若单纯的cin >> s ,若字符串中含有空格,string类型的输入操作符读取字符忽略开头所有的空白字符(如空格,换行符,制表符)直至遇到再次遇到空白字符,读取终止。(也可以while循环中用getline(cin,s))
注意:测试用例不止一个,所以各种问题都要想到,这是程序员的基本功