3

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

    */

}
donjuedo
  • 2,475
  • 18
  • 28
mahin hossen
  • 175
  • 9

3 Answers3

4

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')
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • output is showing same string like the inserted one – mahin hossen Dec 05 '18 at 14:04
  • Yes @mahinhossen I understand that. I am asking what would *you* like the function to do a given string. For example, if I give "FOO" to your function, what would you like the output to be? – gsamaras Dec 05 '18 at 14:05
  • first of all sorry for editing the question,i will edit now it to original one.And secondly there will be two sting input,so i want both string to be changed permanently in same case. – mahin hossen Dec 05 '18 at 14:11
  • i want it to convert string to any particular case,either uppercase or lowercase – mahin hossen Dec 05 '18 at 14:13
  • 1
    i want the function to convert in any case,as given example i want it to convert it to lowercase,got the answer,thanks though.My syntax was wrong – mahin hossen Dec 05 '18 at 14:18
  • 1
    Not only clearer to use the character constants, but now also more portable. Everyone's a winner! – Toby Speight Dec 05 '18 at 14:26
  • 3
    @TobySpeight The "more portable" gets into deeper issues. 1) Aside from ubiquitous ASCII, using `'A'` with EBCDIC, does not help as [A-Z] is not continuous, so the `if()` is broken code. 2) The character encoding of source code and encoding file characters may differ. Still for learners, `'A'` vs `65` is best. – chux - Reinstate Monica Dec 05 '18 at 14:41
  • `+=32` isn't portable either. It's much more straightforward to have an explicit mapping table for a particular character encoding and culture. And, if the character encoding can be (or be compatible with) the execution-charset, literals can be used. It's unfortunate that text-domain exercises are used to teach syntax. – Tom Blodget Dec 05 '18 at 17:07
4

Constant and off-by-1 errors.

  • '65' is a multi byte constant and certainly 65 was meant. @gsamaras. ASCII Z is 90.

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

    // 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'

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

Ideally code would use standard C library functions to detect case.

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

Yet code could call toupper() directly and skip the 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]);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

You're trying to pass a char* to the tolower in scanf("%s",tolower(input1));.

Change it to scanf("%s", input1);

c - convert a mixed-case string to all lower case should tell you how to convert a string to lower case.

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

Abhyudaya Sharma
  • 1,173
  • 12
  • 18
  • 1
    sorry,was trying to do that so that it can take string as lowercase,but it didn't work.forgot to change it while posting here – mahin hossen Dec 05 '18 at 14:07