有没有一种简单的方法可以使用c ++中的ascii代码将小写改为大写? [英] Is there an easy way to change lowercase to uppercase using ascii codes in c++?
问题描述
您好b $ b
我希望找到一种方法,无论使用哪个字母,都可以将任何小写字母更改为大写字母。我在网上看到了一些关于使用asciii值来完成这个的事情,但是我不确定这将如何应用于每个字母。
我相信这会将小写字母b转换为大写字母B,但我怎么能把这个改成
到适用于任何信件?
'B'='b' - 'a'+'A'
我非常感谢您提供的任何建议或帮助。
Hi
I'm hoping to find a way to change any lowercase letter to an uppercase no matter which letter is used. I saw online something about using the asciii values to accomplish this however I'm not sure how this would be applied to every letter.
This I believe will convert lowercase b to uppercase B, but how could I change this
to apply to any letter?
‘B’ = ‘b’ – ‘a’ + ‘A’
I greatly appreciate any advice or help you may offer.
推荐答案
安全的做法是:
The safe way is to do something like:
char c = ...;
if (islower(c)) c=toupper(c);
或
or
wchar_t c = ...;
if (iswlower(c)) c=towupper(c);
另请参阅 http://www.cplusplus.com/reference/cctype/ islower / [ ^ ] char
基于文本,或 http://www.cplusplus.com/reference / cwctype / iswlower / [ ^ ]对于 wchar_t
基于文本。
干杯
Andi
See also http://www.cplusplus.com/reference/cctype/islower/[^] for char
based text, or http://www.cplusplus.com/reference/cwctype/iswlower/[^] for wchar_t
based text.
Cheers
Andi
之间的区别A(65)和a(97)的ASCII值为32.这对所有字母都是相同的。您可以简单地从小写值中减去32,这将是大写等效。
The difference between the ASCII value for A (65) and a (97) is 32. This is the same for all letters. You can simply subtract 32 from the lower case value and that would be the upper case equivalent.
您可以使用这些函数 [ ^ ]或这些 [ ^ ]。或者简单地减去十六进制20(十进制32),如:
You can use these functions[^] or these[^]. Or simply subtract hex 20 (decimal 32) like:
char lowerChar = 'b';
char upperChar = lowerChar - 0x20;
注意你应首先检查角色是否较低上述文档链接中描述的案例。
NB you should check first that the character is lower case as described in the documentation links above.
这篇关于有没有一种简单的方法可以使用c ++中的ascii代码将小写改为大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!