C语言 使用 ASCII 将小写字母转换为大写字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25821178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert lowercase to uppercase using ASCII
提问by Andrei Cusnir
I am trying to convert all lowercase to uppercase letter, using the ASCII table! It is very easy to deal and i have figured out the code. Problem is, that if there is a space between the words, then the program will only change the first word and after the space it will not print anything.
我正在尝试使用 ASCII 表将所有小写字母转换为大写字母!这很容易处理,我已经弄清楚了代码。问题是,如果单词之间有空格,那么程序只会更改第一个单词,而在空格之后不会打印任何内容。
Example
Word: Andreas Gives: ANDREAS
Word: TeSt123Ha Gives: TEST123HA
BUT!!!
Word: Hello 45 Gives: HELLO
after the space it prints nothing!
示例
词:Andreas 给出:ANDREAS
词:TeSt123Ha 给出:TEST123HA
但是!!!
字:你好 45 给出:你好
,在空格之后它什么都不打印!
I know that the space in ASCII table is equal to 32, and in my code i tell the program that if the current code that you are reading is not between 97 and 122, then don't perform any changes!
我知道 ASCII 表中的空间等于 32,在我的代码中我告诉程序,如果您正在阅读的当前代码不在 97 和 122 之间,那么不要执行任何更改!
But it is still not working!
但它仍然无法正常工作!
char currentletter;
int i;
for (i=0; i<49; i++)
{
currentletter = str[i];
if ((currentletter > 96) && (currentletter < 123))
{
char newletter;
newletter = currentletter - 32;
str[i] = newletter;
}
else
{
str[i] = currentletter;
}
}
printf("%s\n", str);
回答by Guest0x20
flipping the 5th lowest bit should help.
翻转第 5 个最低位应该会有所帮助。
Each lowercase letter is 32 + uppercase equivalent. This means simply flipping the bit at position 5 (counting from least significant bit at position 0) inverts the case of a letter. https://web.stanford.edu/class/cs107/lab1/practice.html
每个小写字母等于 32 + 大写字母。这意味着简单地翻转位置 5 处的位(从位置 0 处的最低有效位开始计数)会反转字母的大小写。 https://web.stanford.edu/class/cs107/lab1/practice.html
char *str;
int str_size = sizeof(str);
for(int i=0; i<str_size;i++){
if((str[i]>96) && (str[i]<123)) str[i] ^=0x20;
}
回答by Spikatrix
You have mentioned in one of the commentsthat you use scanf("%s", str);to get the string. The problem is that %swill stop scanning once it finds a whitespace character. In your case, it stops scanning when it sees the space character.
你已经在评论中提及了你用scanf("%s", str);得到的字符串。问题是%s一旦找到空白字符就会停止扫描。在您的情况下,它会在看到空格字符时停止扫描。
Use fgets()if you want to scan one whole line:
使用fgets(),如果你要扫描一个整行:
fgets(str, sizeof(str), stdin);
Once thing to note here is that fgetswill scan in the newline character into the string as well.
这里要注意的一点是,它fgets也会将换行符扫描到字符串中。
Your code can be simplified to:
您的代码可以简化为:
for (int i = 0; str[i] != '#include <ctype.h>
for(int i = 0; str[i] != 'string input = "";
getline(cin, input);
transform(input.begin(), input.end(), input.begin(), [](char c) { return (c > 96 && c < 123) ? c ^= 0x20 : c; });
copy(input.begin(), input.end(), ostream_iterator<char>(cout, " "));
'; i++) // Loop until the NUL-terminator
{
str[i] = tolower(str[i]); // Convert each character to lowercase (It does nothing if the character is not an alphabet)
}
printf("%s\n", str);
'; i++) // Loop until the NUL-terminator
{
if ((str[i] >= 'a') && (str[i] <= 'z')) // If the current character is a lowercase alphabet
str[i] = str[i] - ('a' - 'A'); // See the ASCII table to understand this:
// http://www.asciitable.com/index/asciifull.gif
}
printf("%s\n", str);
Or a more easier way would be to use tolowerfrom ctype.h:
或者更简单的方法是使用tolowerfrom ctype.h:
回答by RioRicoRick
I gave it a try using STL and a Lambda just for fun:
我尝试使用 STL 和 Lambda 只是为了好玩:
##代码##I compiled and tested with c++ 17 in Visual Studio 2019, and did not perform exhaustive testing!
我在Visual Studio 2019中用c++17编译测试,没有进行详尽测试!

