C++ 将字符串的大写字母转换为小写字母,反之亦然

C++ 将字符串的大写字母转换为小写字母,反之亦然,c++,string,C++,String,我正在尝试将字符串字符从大写转换为小写。没有编译错误,但我仍然得到与输入相同的输出: #include <iostream> #include<string.h> #include<ctype.h> using namespace std; int main() { char a[100]; cin>>a; for(int i =0;a[i];i++){ if(islower(a[i])){

我正在尝试将字符串字符从大写转换为小写。没有编译错误,但我仍然得到与输入相同的输出:

#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;
int main() {
    char a[100];
    cin>>a;
    for(int i =0;a[i];i++){
        if(islower(a[i])){
            toupper(a[i]);
        }
        else if(isupper(a[i])){
            tolower(a[i]);
        }
    }
    cout<<a;
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
chara[100];
cin>>a;
for(int i=0;a[i];i++){
if(islower(a[i])){
toupper(a[i]);
}
else if(isupper(a[i])){
托洛尔(a[i]);
}
}

cout
std::toupper
std::tolower
函数不起作用。它们返回结果,因此您必须再次将其分配给
a[i]

char a[100];
std::cin>>a;
for(std::size_t i =0;a[i];i++){
    if(std::islower(a[i])){
        a[i]=std::toupper(a[i]);// Here!
    }
    else if(std::isupper(a[i])){
        a[i]=std::tolower(a[i]);// Here!
    }
}
std::cout<<a;
chara[100];
标准:cin>>a;
对于(std::size_t i=0;a[i];i++){
if(std::islower(a[i])){
a[i]=std::toupper(a[i]);//这里!
}
else if(std::isupper(a[i])){
a[i]=std::tolower(a[i]);//这里!
}
}

std::cout您可以使用标准库中的transform函数和lambda函数,该函数返回给定字符的大写或小写字符

#include <algorithm>
#include <iostream>

using namespace std;


int main
{
    string hello = "Hello World!";
    transform(hello.begin(), hello.end(), hello.begin(), [](char c){
            return toupper(c);})

    cout << hello << endl;
}
#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;

int main() {
    char a[100];
    cin >> a;
    char charConvert;

   for (int i = 0; a[i] ; i++) {

        if  (isupper(a[i])) { 
            charConvert = tolower(a[i]);
        }
        else if (islower(a[i])) {
            charConvert = toupper(a[i]);
        }
    }
    cout << charConvert << endl;

    return 0;
}
#包括
#包括
使用名称空间std;
int main
{
string hello=“hello World!”;
转换(hello.begin(),hello.end(),hello.begin(),[](字符c){
返回到上(c);})

cout我找到了一个解决方案,调用另一个char变量“charConvert”,并将其设置为转换后的字符

#include <algorithm>
#include <iostream>

using namespace std;


int main
{
    string hello = "Hello World!";
    transform(hello.begin(), hello.end(), hello.begin(), [](char c){
            return toupper(c);})

    cout << hello << endl;
}
#include <iostream>
#include<string.h>
#include<ctype.h>
using namespace std;

int main() {
    char a[100];
    cin >> a;
    char charConvert;

   for (int i = 0; a[i] ; i++) {

        if  (isupper(a[i])) { 
            charConvert = tolower(a[i]);
        }
        else if (islower(a[i])) {
            charConvert = toupper(a[i]);
        }
    }
    cout << charConvert << endl;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
chara[100];
cin>>a;
字符转换;
for(int i=0;a[i];i++){
if(isupper(a[i]){
charConvert=tolower(a[i]);
}
else if(islower(a[i])){
charConvert=toupper(a[i]);
}
}

虽然这可以解决问题,但为什么我们要使用“std::”呢我是说我一直使用Turbo C++,在你使用“命名空间STD”的时候,我没有使用它,但是,它不需要“STD::”:但是,这通常是一个坏行为:@ AsiSHoudHauly:麻烦的是Turbo C++是一个非常古老的C++编译器,它预先规定了C++ 98标准,它引入了<代码> STD::/Cult>符号,用于S。代码库可以使用的不是一个好的C++编译器。所有代码都可以用一个代码行使用<代码> C++ +/> >:<代码>包含…STD::转换(A,A+斯特伦(A),A,::ToLoWER);< /Code > @ PulcMcKeZi:实际上,不。它将代码< AB <代码>转换为<代码> AB < /Code >,但结果应该是代码> Ab<代码>。