简体   繁体   English

将小写的字符串转换为大写? 在C中

[英]Convert string of lower case to upper case? In C

My professor gave me some exercises in C language... In one of them I have to pass a string as an argument to a function, this function will verify the existence of lower case letters in the array and convert it into upper case letter; 我的教授给了我一些用C语言进行的练习...在其中之一中,我必须将字符串作为参数传递给一个函数,该函数将验证数组中是否存在小写字母并将其转换为大写字母。

Actually there's a function to do such a thing, but I can't use string.h. 其实有一个函数可以做这样的事情,但是我不能使用string.h。

Does anyone have an idea to do it? 有人有想法吗?

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

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

Would be something like this? 会是这样吗?

you need to include <ctype.h> before using function toupper , then use it like in example below (I edited your code, need to adjust it for your needs): 您需要在使用toupper函数之前包含<ctype.h> ,然后像下面的示例一样使用它(我编辑了您的代码,需要根据需要对其进行调整):

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

this loop will convert 10 first characters to their upper ascii equivalents 此循环会将10个前几个字符转换为它们的上等ascii值

or if you cannot use standard functions, you can use function like this: 或者如果您不能使用标准功能,则可以使用如下功能:

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;
}

If a character is between 'a' and 'z', you could just add ('A' - 'a') to it to convert it to upper. 如果字符在“ a”和“ z”之间,则只需在其上添加('A'-'a')即可将其转换为大写。

char input, output;
int diff = 'A' - 'a';

output = input;
if ('a' <= input && input <= 'z')
  output += diff;

return output;

I guess your professor is expecting something more basic without external functions, like this. 我想您的教授期待没有这种外部功能的更基本的东西。

char str[] = "hello";
int len = sizeof(str) / sizeof(char);
int i;
for(i = 0; i < len; i++) {
    int ascii = str[i];
    if(ascii >= 97 && ascii <= 122) {// 97 => 'a' and 122 => 'z' in ascii
        str[i] = (char) (ascii - 32); // 32 is the ascii substraction of lower 
    }                             // and upper letters 'a' - 'A'
}

Then output would be: 然后输出将是:

HELLO

function will verify the existence of lower case letters in the array and convert it into upper case letter; 函数将验证数组中是否存在小写字母,并将其转换为大写字母;

I can't use string.h. 我不能使用string.h。

Then you have to do conversion yourself. 然后,您必须自己进行转换。 Take a look at the ASCII chart. 看一下ASCII图表。 Then you can notice that small and capital letters are 0x40 apart. 然后您会注意到小写和大写字母相隔0x40 0x40 happens to be space ' ' ; 0x40恰好是空格' ' ;

Loop through your array and convert only the small letters 遍历数组并仅转换小写字母

arr[i] <= 'z' && arr[i] >= 'a'

remember that small and capital letters are ' ' apart. 记得小和大写字母' '分开。

arr[i] = arr[i] - ' ' ;

advance to next character in the array by increasing the index i++ and stop when you encounter the end of the string arr[i]=='\\0' . 通过增加索引i++前进到数组中的下一个字符,并在遇到字符串arr[i]=='\\0'的末尾时停止。

#include <stdio.h>

void converterup(char arr[])
{
    size_t i = 0;

    if(arr == NULL) return;

    while(arr[i]) // loop till the '\0'; this is equivalent to `arr[i]!='\0'`
    {
        if(arr[i] <= 'z' && arr[i] >= 'a'){
          arr[i] = arr[i] - ' ' ;
        }

        i++;
    }
}

int main(void)
{
    char str[] = "Hello World!";

    converterup(str);

    printf("%s",str);

    return 0;
}

Test: 测试:

HELLO WORLD! 

Check out my code guys, is it acceptable? 看看我的代码专家,可以接受吗?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

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

        for(i = 0;palavra[i] > 60 && palavra[i] < 122; i++)
        {
            printf("%c", palavra[i] - 32);
        }
    }

    int main(void)
    {
        setlocale(LC_ALL, "");
        char palavra[10];

            printf("Insira uma palavra maiúsculas: "); gets(palavra);
            printf("Valor com conversão: ");
            strupr(palavra);


        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM