gpt4 book ai didi

ascii - 在没有 ctype.h 的情况下转换小写/大写字母

转载 作者:行者123 更新时间:2023-12-04 05:24:21 52 4
gpt4 key购买 nike

我刚刚看到这在技术上是可行的,我无法解决的唯一错误是每次测试时打印的最后一个 ASCII 字符,我也在不使用 的情况下进行了测试。姓名 变量,我的意思是在 ASCII 中的任何小写字母减去 32 应该给我他们的大写字母并且确实如此,但我很好奇为什么我得到一个额外的字符,这与我在屏幕上看到的不同显然是 Û .

#include <stdio.h>
main()
{
char name[22];
int i;

fputs("Type your name ",stdout);
fgets(name,22,stdin);


for (i = 0; name[i] != '\0'; i = i + 1)
printf("%c",(name[i])-32); /*This will convert lower case to upper */
/* using as reference the ASCII table*/
fflush(stdin);
getchar();
}

最佳答案

也许在字符串的末尾有一个换行符。

您可以检查字符代码,以便只转换实际上是小写字母的字符:

for (i = 0; name[i] != '\0'; i = i + 1) {
char c = name[i];
if (c => 97 && c <= 122) {
c -= 32;
}
printf("%c", c);
}

关于ascii - 在没有 ctype.h 的情况下转换小写/大写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13370660/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com