编写一个单词转换函数,该函数具有一个char*参数。函数的功能:将参数代表的字符串中的每个单词的第一个字母转换为大写字母,并显示转换后的字符串。例如;假设函数参数字符串如下:
There are 100 students in the room.
那么采用函数转换以后,该字符串为:
There Are 100 Students In The Room.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
void toUp(char* str)
{
for(int i = 0; i < strlen(str); i++){
if(str[0] >= 'a' && str[0] <= 'z') str[0] -= 32;
if(str[i-1] == ' ' && str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
}
int p;
for(int i = 0;i < strlen(str); i++){
if(str[i]!=' '){
p = i;
break;
}
}
for(int i = p;i < strlen(str); i++){
if(str[i]==' ' && str[i+1]== ' '){
continue;
}
cout<<str[i];
}
}
int main(){
string str;
getline(cin,str);
toUp(const_cast<char *>(str.c_str()));
return 0;
}