如何在C++中将小写字符串转换为大写字符串

时间:2021-03-13 06:46:56

标签: c++ arrays for-loop while-loop uppercase

嗨,我想创建一个程序,在其中输入不同的名称,然后以大写形式输出。但是我的代码中有一个错误,你能帮我弄清楚吗?

它说“[Error] no match for 'operator>='(操作数类型是 'std::string {aka std::basic_string}' 和 '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";
        }
        
        

1 个答案:

答案 0 :(得分:0)

您应该将 const char* 传递给 strlen() 函数。参考:https://www.programiz.com/cpp-programming/library-function/cstring/strlen

但是'name'是一个字符串类型的数组。因此编译器返回一个错误。

name[j] 的数据类型也是 std::string。因此,您不能将 name[j] 与整数进行比较。

如果您需要将字符串转换为大写字符串,请使用下面的代码块(str是一个字符串变量)。

std::transform(uppers.begin(), uppers.end(), uppers.begin(), [](unsigned char c){ return std::toupper(c); });
相关问题