我的教授给了我一些C语言的练习。。。在其中一个中,我必须将字符串作为参数传递给函数,该函数将验证数组中小写字母的存在并将其转换成大写字母;
实际上有一个函数可以做这样的事情,但是我不能使用string.h。
有人想这么做吗?

void converterup(char palavra[])
{
    int i;

    for(i = 0; i < 10; i++)
    {
        if(palavra[i] != 'a')
        {
            palavra[i] == 'A';
        }
    }

会是这样吗?

最佳答案

在使用函数<ctype.h>之前,您需要包含toupper,然后像下面的示例中那样使用它(我编辑了您的代码,需要根据您的需要调整它):

for(i = 0; i < 10; i++){
    palavra[i] = toupper(palavra[i]);
}

这个循环将把10个第一个字符转换为它们的高位ascii等价字符
或者如果不能使用标准函数,可以使用如下函数:
char myUpperChar(char x){
    const int delta = 'a' - 'A'; //'a' has ascii code 97 while 'A' has code 65
    if(x <= 'z' && x >= 'a'){
        x -= delta;
    }
    return x;
}

10-08 09:47