简体   繁体   English

使用C中的函数将大写字符串转换为小写的程序中的问题

[英]Problem in a program to convert upper case string to lowercase using function in C

I am trying to write a program which will only compare same case letter, but first it should convert any case to a particular case. 我正在尝试编写一个程序,它只会比较相同的大小写字母,但首先它应该将任何案例转换为特定的大小写。

But I am having trouble converting the string to any particular case using a function, though I have figured out how to do that without a function. 但是我在使用函数将字符串转换为任何特定情况时遇到了问题,尽管我已经想出了如何在没有函数的情况下执行此操作。 Here's my code. 这是我的代码。 Can anyone help me find out where I am going wrong? 任何人都可以帮我找出我错的地方吗?

#include<stdio.h>
#include<string.h>
void order(char input[])
{
    int i=0;

    while(input[i]!='\0')
    {
        if(input[i]>'65' && input[i]<'92')
        {
            input[i]+=32;
           // printf("WORKING\n");
        }

        i++;

    }
}
int main()
{
    char input1[101], output1[101];
    char input2[101], output2[101];
    int r;

    scanf("%s",tolower(input1));
    scanf("%s",input2);

    order(input1);
    order(input2);

    printf("%s\n",input1);
    printf("%s\n",input2);

    /*

    r=strcmp(output1,output2);


    if(r<0)
        printf("-1\n");
    else if(r>0)
        printf("1\n");
    else
        printf("0\n");

    */

}

Change this: 改变这个:

if(input[i]>'65' && input[i]<'92')

to this: 对此:

if(input[i] >= 65 && input[i] <= 90)

since you want to check for the ASCII codes of A and Z. Notice that because you used magic numbers, you made a mistake for both. 因为您要检查A和Z的ASCII码。请注意,因为您使用了幻数,所以两者都犯了错误。 I had to check an ASCII table to figure that out that you needed the equality sign as well, and 90 (code for Z), instead of 92 (code for '\\'). 我必须检查一个ASCII表,以确定你需要等号,90(代码为Z),而不是92(代码为'\\')。

I suggest you then to use characters constants, like this: 我建议你使用字符常量,如下所示:

if(input[i] >= 'A' && input[i] <= 'Z')

Constant and off-by-1 errors. 常数和失1错误。

  • '65' is a multi byte constant and certainly 65 was meant. '65'是一个多字节常数,当然意味着65 @gsamaras . @gsamaras ASCII Z is 90. ASCII Z是90。

     // if(input[i]>'65' && input[i]<'92') if(input[i]>65 && input[i]< Zee) 
  • Off by 1 due to > vs. >= 由于> vs. >=而关闭1

     // if(input[i]>'65' && input[i]<'92') // if(input[i]>65 && input[i]< Zee) if(input[i] >= 65 && input[i] <= 90) 

Character constants are more self documenting. 字符常量更自我记录。 Consider 'A' 考虑'A'

    if(input[i] >= 'A' && input[i] <= 'Z')

Ideally code would use standard C library functions to detect case. 理想情况下,代码将使用标准C库函数来检测大小写。

    if(isupper((unsigned char) input[i]))

Yet code could call toupper() directly and skip the if() . 然而代码可以直接调用toupper()并跳过if()

while(input[i]) {
  input[i] = tolower((unsigned char) input[i]);
  i++;
}

// or 

for (size_t i = 0; input[i]; i++) {
  input[i] = tolower((unsigned char) input[i]);
}

You're trying to pass a char* to the tolower in scanf("%s",tolower(input1)); 你试图将一个char*传递给scanf("%s",tolower(input1));tolower scanf("%s",tolower(input1)); .

Change it to scanf("%s", input1); 将其更改为scanf("%s", input1);

c - convert a mixed-case string to all lower case should tell you how to convert a string to lower case. c - 将一个混合大小写的字符串转换为所有小写字母应该告诉你如何将字符串转换为小写。

Take a look at https://linux.die.net/man/3/tolower and https://linux.die.net/man/3/toupper . 请查看https://linux.die.net/man/3/tolowerhttps://linux.die.net/man/3/toupper

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

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