大写功能无法正常工作
Capitalize function not working properly
我正在学习 C++ 的基础知识,我正在尝试编写一个简单的函数,将给定输入中每个单词的每个字母大写。我写的:
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
int main()
{
std::cout << "Please enter a sentence: ";
std::vector<std::string> words;
std::string x;
while (std::cin >> x) {
words.push_back((std::string) x);
}
std::cout << std::endl;
std::vector<std::string>::size_type size;
size = words.size();
for (int j = 0; j != size; j++) {
std::string &r = words[j];
for (int i = 0; i != r.length(); i++) {
r = toupper(r[i]);
std::cout << r << std::endl;
}
}
}
returns 每个单词的第一个字母大写。例如,如果我写 hello world,程序 returns:
H
W
谁能告诉我我做错了什么以及如何解决它。
你对每个词的处理都是错误的:
for (int i = 0; i != r.length(); i++) {
r = toupper(r[i]);
std::cout << r << std::endl;
}
你真正需要的只是修改第一个字母:
r[0] = toupper(r[0]);
std::cout << r << '\n';
作为简化,您的循环:
std::vector<std::string>::size_type size;
size = words.size();
for (int j = 0; j != size; j++) {
std::string &r = words[j];
可以更简洁:
for (std::string &r : words) {
for (int j = 0; j != size; j++) {
std::string &r = words[j];
for (int i = 0; i != r.length(); i++) {
r = toupper(r[i]);
std::cout << r << std::endl;
}
}
在 r = toupper(r[i]);
处,您将 r
覆盖为长度为 1 的字符串。因此您的内层 for
循环条件变为假,并且您退出了内层循环。所以只打印出每个单词的第一个字母。
要解决此问题,请将 toupper
的 return 值保存到其他变量。
for (int j = 0; j != size; j++) {
std::string &r = words[j];
for (int i = 0; i != r.length(); i++) {
char c = toupper(r[i]);
std::cout << c << std::endl;
}
}
我有一个实用程序 class,它只包含 static
用于执行字符串操作的函数或方法。这是我的 class 使用 toUpper
和 toLower
静态方法的样子:
实用程序
#ifndef UTILITY_H
#define UTILITY_H
#include <string>
class Utility {
public:
static std::string toUpper( const std::string& str );
static std::string toLower( const std::string& str );
private:
Utility();
};
#endif // UTILITY_H
#include "Utility.h"
#include <algorithm>
std::string Utility::toUpper( const std::string& str ) {
std::string result = str;
std::transform( str.begin(), str.end(), result.begin(), ::toupper );
return result;
}
std::string Utility::toLower( const std::string& str ) {
std::string result = str;
std::transform( str.begin(), str.end(), result::begin(), ::tolower );
return result;
}
用法:
#include <string>
#include <iostream>
#include "Utility.h"
int main() {
std::string strMixedCase = std::string( "hEllO WOrlD" );
std::string lower = Utility::toLower( strMixedCase );
std::string upper = Utility::toUpper( strMixedCase );
std::cout << lower << std::endl;
std::cout << upper << std::endl;
return 0;
}
注意: - 这会对传入的字符串进行完整的字符串操作。如果您尝试对字符串中的特定字符进行操作;您可能需要做一些不同的事情,但这是如何将 <algorithm>'s
std::transform()
与 ::toupper
和 ::tolower
一起使用的开始
我正在学习 C++ 的基础知识,我正在尝试编写一个简单的函数,将给定输入中每个单词的每个字母大写。我写的:
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
int main()
{
std::cout << "Please enter a sentence: ";
std::vector<std::string> words;
std::string x;
while (std::cin >> x) {
words.push_back((std::string) x);
}
std::cout << std::endl;
std::vector<std::string>::size_type size;
size = words.size();
for (int j = 0; j != size; j++) {
std::string &r = words[j];
for (int i = 0; i != r.length(); i++) {
r = toupper(r[i]);
std::cout << r << std::endl;
}
}
}
returns 每个单词的第一个字母大写。例如,如果我写 hello world,程序 returns:
H
W
谁能告诉我我做错了什么以及如何解决它。
你对每个词的处理都是错误的:
for (int i = 0; i != r.length(); i++) {
r = toupper(r[i]);
std::cout << r << std::endl;
}
你真正需要的只是修改第一个字母:
r[0] = toupper(r[0]);
std::cout << r << '\n';
作为简化,您的循环:
std::vector<std::string>::size_type size;
size = words.size();
for (int j = 0; j != size; j++) {
std::string &r = words[j];
可以更简洁:
for (std::string &r : words) {
for (int j = 0; j != size; j++) {
std::string &r = words[j];
for (int i = 0; i != r.length(); i++) {
r = toupper(r[i]);
std::cout << r << std::endl;
}
}
在 r = toupper(r[i]);
处,您将 r
覆盖为长度为 1 的字符串。因此您的内层 for
循环条件变为假,并且您退出了内层循环。所以只打印出每个单词的第一个字母。
要解决此问题,请将 toupper
的 return 值保存到其他变量。
for (int j = 0; j != size; j++) {
std::string &r = words[j];
for (int i = 0; i != r.length(); i++) {
char c = toupper(r[i]);
std::cout << c << std::endl;
}
}
我有一个实用程序 class,它只包含 static
用于执行字符串操作的函数或方法。这是我的 class 使用 toUpper
和 toLower
静态方法的样子:
实用程序
#ifndef UTILITY_H
#define UTILITY_H
#include <string>
class Utility {
public:
static std::string toUpper( const std::string& str );
static std::string toLower( const std::string& str );
private:
Utility();
};
#endif // UTILITY_H
#include "Utility.h"
#include <algorithm>
std::string Utility::toUpper( const std::string& str ) {
std::string result = str;
std::transform( str.begin(), str.end(), result.begin(), ::toupper );
return result;
}
std::string Utility::toLower( const std::string& str ) {
std::string result = str;
std::transform( str.begin(), str.end(), result::begin(), ::tolower );
return result;
}
用法:
#include <string>
#include <iostream>
#include "Utility.h"
int main() {
std::string strMixedCase = std::string( "hEllO WOrlD" );
std::string lower = Utility::toLower( strMixedCase );
std::string upper = Utility::toUpper( strMixedCase );
std::cout << lower << std::endl;
std::cout << upper << std::endl;
return 0;
}
注意: - 这会对传入的字符串进行完整的字符串操作。如果您尝试对字符串中的特定字符进行操作;您可能需要做一些不同的事情,但这是如何将 <algorithm>'s
std::transform()
与 ::toupper
和 ::tolower