从键盘输入字符串,将其中的单词(两个空格之间的字符串)的第一个字母转换为大写字母 10
要求:单词的第一个字母转换为大写字母由一个单独的函数完成
c++ 展开
#include <iostream>
#include <iterator>
#include <cctype>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
string capitalize(string &s)
{
if(s.empty()) return s;
string cap_str(s);
s[0] = std::toupper(s[0]);
return cap_str;
}
int main()
{
vector<string> data;
cout << "请输入字符串,以空格分隔,Ctrl+Z结束: ";
copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(data));
cout << "原始字符串为: ";
copy(data.begin(), data.end(), ostream_iterator<string>(cout, "、"));
cout << endl;
for_each(data.begin(), data.end(), capitalize);
cout << "新字符串为: ";
copy(data.begin(), data.end(), ostream_iterator<string>(cout, "、"));
cout << endl;
return 0;
}
void main()
{
char str[1024];
printf("输入字符串:\n");
gets(str);
int i;
for (i=0;str[i] != '*';i++)
{
if((i==0)&&(str[i]>='a'&&str[i]<='z'))
str[i]=str[i]-32;
if ((str[i] == ' ')&&(str[i+1]>='a'&&str[i+1]<='z'))
{
str[i+1]=str[i+1]-32;
}
printf("%c",str[i]);
}
}