Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

function

string

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");

    */

}
like image 910
mahin hossen Avatar asked Dec 08 '25 11:12

mahin hossen


1 Answers

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. 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 '\').

I suggest you then to use characters constants, like this:

if(input[i] >= 'A' && input[i] <= 'Z')
like image 63
gsamaras Avatar answered Dec 10 '25 00:12

gsamaras