如何更改 C++ 中字符串的大小写?

发布于 2024-09-30 13:15:28 字数 62 浏览 0 评论 0原文

我有一个可能包含数字以及大写和小写字母的字符串。我需要将所有大写字母转换为小写字母,反之亦然。人们会怎样做呢?

I have a string that may contain numbers as well as upper and lower case letters. I need to convert all the uppercase letters to lowercase and vice versa. How would one go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

So要识趣 2024-10-07 13:15:28

这是一种无需提升即可实现的方法:

#include <string>
#include <algorithm>
#include <cctype>
#include <iostream>

char change_case (char c) {
    if (std::isupper(c)) 
        return std::tolower(c); 
    else
        return std::toupper(c); 
}

int main() {
    std::string str;
    str = "hEllo world";
    std::transform(str.begin(), str.end(), str.begin(), change_case);
    std::cout << str;
    return 0;
}

Here's a way to do it without boost:

#include <string>
#include <algorithm>
#include <cctype>
#include <iostream>

char change_case (char c) {
    if (std::isupper(c)) 
        return std::tolower(c); 
    else
        return std::toupper(c); 
}

int main() {
    std::string str;
    str = "hEllo world";
    std::transform(str.begin(), str.end(), str.begin(), change_case);
    std::cout << str;
    return 0;
}
拍不死你 2024-10-07 13:15:28

迭代字符串并使用 isupper() 来确定每个字符是否为大写。如果是大写,请使用 tolower() 将其转换为小写。如果不是大写,请使用 toupper() 将其转换为大写。

Iterate the string and use isupper() to determine if each character is uppercase or not. If it's uppercase, convert it to lowercase using tolower(). If it's not uppercase, convert it to uppercase using toupper().

心头的小情儿 2024-10-07 13:15:28

您可以迭代字符串并从每个字母字符中添加或减去适当的数字,以便将 ASCII 值解析为相反大小写的 ASCII 值。

You can iterate through the string and add or subtract the appropriate number from each alpha character so that the ASCII value is resolved to the opposite case ASCII value.

负佳期 2024-10-07 13:15:28

您也可以翻转 32 位:适用于 ASCII 字符,因此:

char flipcase(char x)
{
   if( ::isalpha(x) )
   {
      return x ^ 32;
   }
}

您还可以使用表格。然后创建一个静态表后

char flipcase( char x )
{
   return fliptable[x];
}

一种方法的优点是也可以用于外来字符集,只要它们不是多字节的 - 它对此不起作用。您可以使用您使用的任何字符集为 wchar_t 字符创建一个类似的表。如果字符大小不超过 2,存储表需要少量内存,但对于 UTF-32 来说会使用太多内存,而且查找时间也很短。当然,实际上您会为每个角色及其特征存储一个小结构。

对于上述任何一个,您都可以使用 std::transform。

现在真正聪明的一点是:std::transform 可以使用类(函子)以及普通函数。因此,对于多字节字符,只要替换字符始终具有相同的第一个元素,我们就可以存储状态。如果不是这种情况,则转换将无法与常规迭代器一起使用。但是,您可以编写一个自定义迭代器来处理多字节字符串,该字符串一次迭代一个可打印字符(迭代器必须取消引用表示符号的多字节字符)。

You can flip the 32 bit as well: works with ASCII characters thus:

char flipcase(char x)
{
   if( ::isalpha(x) )
   {
      return x ^ 32;
   }
}

You can also use a table. Create a static table then

char flipcase( char x )
{
   return fliptable[x];
}

The latter method having the advantage of being able to be used for foreign character sets too as long as they are not multi-byte - it won't work for that. You can make a similar table for wchar_t characters with whatever character set you are using. Storing the table requires a small amount of memory use if sizeof your character is no more than 2, although it would use too much for UTF-32 and the lookup time is trivial. Of course in reality you would store a small struct for each character with its traits.

With either of the above you would use std::transform.

And now the really clever bit: std::transform can use a class (functor) as well as a plain function. For multi-byte characters we can therefore store the state as long as the replacement character will always have the same first element. If that is not the case then transform won't work with the regular iterators. However you could write a custom iterator to handle multi-byte character strings that would iterate up a printable character at a time (The iterator would have to dereference to multi-byte characters representing a symbol).

仲春光 2024-10-07 13:15:28
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main(){
    //Changecase to Uppercase
    string var = "This is a String.";
    for (unsigned int i = 0; i < var.length(); i++){
        if (islower(var[i]))
            var[i] = toupper(var[i]);
    }
    cout << var << endl;
    /*
    Output:
    THIS IS A STRING.
    */
    //Changecase to Lowercase
    var = "This is a String.";
    for (unsigned int i = 0; i < var.length(); i++){
        if (isupper(var[i]))
            var[i] = tolower(var[i]);
    }
    cout << var << endl;
    /*
    Output:
    this is a string.
    */
    //Flip the case
    var = "This is a String.";
    for (unsigned int i = 0; i < var.length(); i++){
        if (isupper(var[i]))
            var[i] = tolower(var[i]);
        else
            var[i] = toupper(var[i]);
    }
    cout << var << endl;
    /*
    Output:
    tHIS IS A sTRING.
    */

    system("pause");
    return 0;
}
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main(){
    //Changecase to Uppercase
    string var = "This is a String.";
    for (unsigned int i = 0; i < var.length(); i++){
        if (islower(var[i]))
            var[i] = toupper(var[i]);
    }
    cout << var << endl;
    /*
    Output:
    THIS IS A STRING.
    */
    //Changecase to Lowercase
    var = "This is a String.";
    for (unsigned int i = 0; i < var.length(); i++){
        if (isupper(var[i]))
            var[i] = tolower(var[i]);
    }
    cout << var << endl;
    /*
    Output:
    this is a string.
    */
    //Flip the case
    var = "This is a String.";
    for (unsigned int i = 0; i < var.length(); i++){
        if (isupper(var[i]))
            var[i] = tolower(var[i]);
        else
            var[i] = toupper(var[i]);
    }
    cout << var << endl;
    /*
    Output:
    tHIS IS A sTRING.
    */

    system("pause");
    return 0;
}
画▽骨i 2024-10-07 13:15:28

执行此操作的标准 C++ 方法如下:

std::string lower = "this is my string to be uppercased";
std::locale loc; //get the locale from the system.
auto facet = std::use_facet<std::ctype<char>>(loc);
facet.toupper(&lower[0], &lower[0] + lower.length()); //they didn't provide a C++ iterator interface, so pointer arithmetic it is.  
std::cout << lower << std::endl;

此输出:

 THIS IS MY STRING TO BE UPPERCASED

还有一个 facet.toupper() 来处理小写到大写的转换,以及 facet.widen 来转换char 到 wchar_t,以及 facet.narrow 从 wchar_t 转换为 char。

The standard C++ method to do this is as follows:

std::string lower = "this is my string to be uppercased";
std::locale loc; //get the locale from the system.
auto facet = std::use_facet<std::ctype<char>>(loc);
facet.toupper(&lower[0], &lower[0] + lower.length()); //they didn't provide a C++ iterator interface, so pointer arithmetic it is.  
std::cout << lower << std::endl;

this outputs:

 THIS IS MY STRING TO BE UPPERCASED

there's also a facet.toupper() to handle converting lowercase to uppercase, and facet.widen to convert from char to wchar_t, and facet.narrow to convert from wchar_t to char.

春花秋月 2024-10-07 13:15:28
char *str = "this is a test string";
while(*str != '\0') {
    if(*str <= 'Z' && *str >= 'A') {
       *str += 32;
    } else if(*str >= 'a' && *str <= 'z') {
       *str -= 32;
    }
 str++;
}

一点也不安全,只是给你一个想法。
这可能对本作业非常有帮助:

alt text

char *str = "this is a test string";
while(*str != '\0') {
    if(*str <= 'Z' && *str >= 'A') {
       *str += 32;
    } else if(*str >= 'a' && *str <= 'z') {
       *str -= 32;
    }
 str++;
}

Not secure at all, just giving you the idea.
This might be very helpful for this homework:

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文