自已拟定一个解决实际问题的题目用动态联编机制编程实现程序存为exp_508cpp
题目:设计一个程序,实现将一段英文文本中的所有单词首字母大写的功能。
实现思路:读入英文文本,将每个单词的首字母转换为大写,输出修改后的文本。
代码如下:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void capitalize(string& s) {
bool new_word = true;
for (int i = 0; i < s.length(); i++) {
if (new_word && islower(s[i])) {
s[i] = toupper(s[i]);
}
new_word = isspace(s[i]);
}
}
int main() {
string text;
getline(cin, text);
capitalize(text);
cout << text << endl;
return 0;
}
运行结果:
输入:
hello world. this is a test.
输出:
Hello World. This Is A Test.
``
原文地址: https://www.cveoy.top/t/topic/eQog 著作权归作者所有。请勿转载和采集!