#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string str;
while(getline(cin,str))
{
int i=0;
int len=str.length();
if(islower(str[0]))
str[0]=toupper(str[0]);
for(i=1; i<len; i++)
{
if(str[i-1]=' '&&islower(str[i]))
str[i]=toupper(str[i]);
}
cout<<str<<endl;
}
return 0;
}
本题主要是要注意几点
多掌握一些c++库函数
比如#include<cctyoe>
下就有多种可用的函数
c++中应该是#include <cctype>
c中应该是#include <ctype.h>![](https://img-blog.csdn.net/20170325151154289?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2luYXRfMzc3NTU0MDY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
以下为字符函数库中常用的函数:
常用的有:
tolower()——toupper()
isupper()——islower()
isalnum()——isalpha()(注:表格来自网咯)
第二计算长度的str.length()和strlen(str)区别
两者都是求字符串的长度,但strlen( )的参数必须是char* ;而 str.length( )是string类对象str调用的成员函数,所以它们用在不同的地方;
char* ch="asdfsafas";
string str="adfadf";
cout<<str.length();
// cout<<strlen(str); 出错
cout<<strlen(ch);
// cout<<ch.length();出错
hduoj 2026 首写字母变大写
最新推荐文章于 2020-04-22 14:41:00 发布