如何更改 C++ 中字符串的大小写?
我有一个可能包含数字以及大写和小写字母的字符串。我需要将所有大写字母转换为小写字母,反之亦然。人们会怎样做呢?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是一种无需提升即可实现的方法:
Here's a way to do it without boost:
迭代字符串并使用 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 usingtolower()
. If it's not uppercase, convert it to uppercase usingtoupper()
.您可以迭代字符串并从每个字母字符中添加或减去适当的数字,以便将 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.
您也可以翻转 32 位:适用于 ASCII 字符,因此:
您还可以使用表格。然后创建一个静态表后
一种方法的优点是也可以用于外来字符集,只要它们不是多字节的 - 它对此不起作用。您可以使用您使用的任何字符集为 wchar_t 字符创建一个类似的表。如果字符大小不超过 2,存储表需要少量内存,但对于 UTF-32 来说会使用太多内存,而且查找时间也很短。当然,实际上您会为每个角色及其特征存储一个小结构。
对于上述任何一个,您都可以使用 std::transform。
现在真正聪明的一点是:std::transform 可以使用类(函子)以及普通函数。因此,对于多字节字符,只要替换字符始终具有相同的第一个元素,我们就可以存储状态。如果不是这种情况,则转换将无法与常规迭代器一起使用。但是,您可以编写一个自定义迭代器来处理多字节字符串,该字符串一次迭代一个可打印字符(迭代器必须取消引用表示符号的多字节字符)。
You can flip the 32 bit as well: works with ASCII characters thus:
You can also use a table. Create a static table then
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).
执行此操作的标准 C++ 方法如下:
此输出:
还有一个
facet.toupper()
来处理小写到大写的转换,以及facet.widen
来转换char 到 wchar_t,以及facet.narrow
从 wchar_t 转换为 char。The standard C++ method to do this is as follows:
this outputs:
there's also a
facet.toupper()
to handle converting lowercase to uppercase, andfacet.widen
to convert from char to wchar_t, andfacet.narrow
to convert from wchar_t to char.一点也不安全,只是给你一个想法。
这可能对本作业非常有帮助:
Not secure at all, just giving you the idea.
This might be very helpful for this homework: