gpt4 book ai didi

c - 重新编码 Toupper 函数

转载 作者:行者123 更新时间:2023-11-30 15:28:14 27 4
gpt4 key购买 nike

我被分配重新编码 toUpper 和 toLower 函数。我是否必须在 if 语句中写出所有 26 个字母,例如:

if ( char == 'a' )
return 'A';

或者还有其他简单的方法吗?

最佳答案

Lower-case ASCII characters are sequential ,所以这是一个简单的情况,如果字符的值在给定范围内,则对其进行偏移:

#include <stdio.h>

int to_upper(int value) { return (value >= 'a' && value <= 'z') ? value - ('a'-'A') : value; }

int main(int argc, char *argv[])
{
printf("%c and %c\n", to_upper('B'), to_upper('f'));
return 0;
}

在线尝试此代码 here .

关于c - 重新编码 Toupper 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26690292/

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