Convert a String In C++ To Upper Case

后端 未结 30 1547
一个人的身影
一个人的身影 2020-11-22 05:25

How could one convert a string to upper case. The examples I have found from googling only have to deal with chars.

30条回答
  •  不思量自难忘°
    2020-11-22 06:01

    The faster one if you use only ASCII characters:

    for(i=0;str[i]!=0;i++)
      if(str[i]<='z' && str[i]>='a')
        str[i]+='A'-'a';
    

    Please note that this code run faster but only works on ASCII and is not an "abstract" solution.

    Extended version for other UTF8 alphabets:

    ...
    if(str[i]<='z' && str[i]>='a') //is latin
        str[i]+='A'-'a';
    else if(str[i]<='я' && str[i]>='а') //cyrillic
        str[i]+='Я'-'я'
    else if(str[i]<='ω' && str[i]>='α') //greek
        str[i]+='Ω'-'ω'
    //etc...
    

    If you need full UNICODE solutions or more conventional and abstract solutions, go for other answers and work with methods of C++ strings.

提交回复
热议问题