[英]How to convert lower case string to uppercase string in C++
Hi I want to create a program where I will input different names and I will output it in UPPERCASE FORM.嗨,我想创建一个程序,我将在其中输入不同的名称,我将 output 以大写形式。 However there's a bug in my code, can you help me to figure it out?
但是我的代码中有一个错误,你能帮我弄清楚吗?
It said "[Error] no match for 'operator>=' (operand types are 'std::string {aka std::basic_string}' and 'int')"它说“[Error] no match for 'operator>=' (operand types are 'std::string {aka std::basic_string}' and 'int')”
int times;
string name [1000];
int i = 1;
int hold = 0;
int j ;
int cont;
while( i != 0){
cout<<"Enter Name "<<endl;
cin>>name[hold];
times = hold;
for ( j = 0 ; j <= strlen(name) ; j++){
if (name[j] >= 97 && name[j] <= 122){
name[j] = name[j] -32;
}
}
cout<<"\n[1] for Add"<<endl;
cout<<"[2] for Stop"<<endl;
cin>>cont;
if ( cont == 1){
hold++;
i = 1;
}else{
i = 0;
}
}
for ( i = 0 ; i <= times ; i++){
cout<<name[i]<<"\n";
}
You should pass a const char*
to the strlen() function.您应该将
const char*
传递给 strlen() function。 Refer: https://www.programiz.com/cpp-programming/library-function/cstring/strlen参考: https://www.programiz.com/cpp-programming/library-function/cstring/strlen
But the 'name' is an string type array.但是“名称”是一个字符串类型的数组。 Therefore the compiler returns an error.
因此编译器返回一个错误。
Also the data type of name[j] is std::string. name[j] 的数据类型也是 std::string。 Therefore you cannot compare name[j] with an integer.
因此,您不能将 name[j] 与 integer 进行比较。
If you need to convert a string to uppercase string, please use the below code block (str is a string variable).如果您需要将字符串转换为大写字符串,请使用下面的代码块(str 是字符串变量)。
std::transform(uppers.begin(), uppers.end(), uppers.begin(), [](unsigned char c){ return std::toupper(c); });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.