2

At school Im working on a palindrome C program. I'm almost done, but I would like my program to mark both 'Anna' and 'anna' as a palindrome. I tried some stuff out but nothing really worked. My code :

#include <stdio.h>
#include <string.h>

int main() {
    char palindroom[50],a;
    int lengte, i;
    int woord = 0;

    printf("This program checks if your word is a palindrome.\n");
    printf("Enter your word:\t");
    scanf("%s", palindroom);

    lengte = strlen(palindroom);

    for (i = 0; i < lengte; i++) {
        if (palindroom[i] != palindroom[lengte - i - 1]) {
            woord = 1;
            break;
        }
    }

    if (woord) {
        printf("Unfortunately, %s is not palindrome\n\n", palindroom);
    }
    else {
            printf("%s is a palindrome!\n\n", palindroom);
    }
    getchar();
    return 0;
}

I've seen some people using tolower from ctype.h but I'd like to avoid that.

So my question is : how do I convert all uppers to lowers in a string?

[ps. some words I may code might seem odd, but that's Dutch. Just erase an o and you'll understand]

Thanks.

DeDaymar
  • 23
  • 4
  • 4
    If you want to do what `tolower()` is meant for, but not use it, then please give reasons. – Yunnosch Jan 07 '18 at 19:35
  • "How do I convert all uppers to lowers in a string?" You can use `strlwr` but I don't think it is a standard C library function. – Weather Vane Jan 07 '18 at 19:37
  • I agree with @Yunnosch in that you have to give a reason, because whatever you do (if you do it in a sane way) will mimic the functionality of `tolower` – klutt Jan 07 '18 at 19:40
  • 2
    If the reason for not wanting to use `tolower()` is "teacher forbids", then the teacher wants you to apply something you have been shown in recent parts of the class you are attending. Can you find something helpful by reading the most recent chapter of your textbook? – Yunnosch Jan 07 '18 at 19:41
  • 2
    "not use tolower" is not sufficient as requirements for the solution. You need to provide the context. Otherwise assumptions on localisation, in-scope cases, edge cases, error-handling etc. will have to be made by guessing. And will probably fail. Also, whatever the old hands here at StackOveflow might propose (even with all kinds of detailed requirements) might get you in trouble via "How did you come up with that solution, which is not covered by what I taught you?". So I consider this question off-topic as too broad, maybe as unclear. – Yunnosch Jan 07 '18 at 19:47
  • If teacher forbids using the library functions, please tell teacher that ASCII is common but not required. So use the library functions please. – Weather Vane Jan 07 '18 at 20:55
  • Why would a teacher forbid the usage of the standard library? I've seen lots of posts here where they say "teacher doesn't allow this or that". What's the point? – Pablo Jan 07 '18 at 20:58
  • 1
    @Pablo probably the point is to give the students a chance to recognize that you can add and subtract characters. Also, sometimes :-) the standard libraries are *not* available. Or perhaps you have to implement them :) would come in handy if you know how to do things *without* the standard library – Ahmed Masud Jan 07 '18 at 21:07
  • Yes you guys got it quite right, I don't think my teacher will trust me if I would say that I wrote a program with code like that. So I just try to find easier solutions. This way my teacher can completely trust me and also I can explain everything I put in my code. So yes @Yunnosch you summed it up pretty well – DeDaymar Jan 07 '18 at 21:09

2 Answers2

0

If you want don't want to use tolower or toupper you can do this:

// tolower
char c = 'U';
char lower_u = c | 0x20

// toupper
char c = 'u';
char upper_u = c & 0xdf

In ASCII the difference between a lower and an upper character is the 5th bit. When The 5th bit is 0, you get an upper character, when the 5th bit is 1, you get a lower character.

Pablo
  • 13,271
  • 4
  • 39
  • 59
0

the difference between uppercase and lowercase in ASCII table is 32 so you can add 32 if an uppercase letter is in the input to convert it to lowercase ( http://www.asciitable.com/ ) :

if ((currentletter > 64) && (currentletter < 91))
    {
        char newletter;
        newletter = currentletter + 32;
        str[i] = newletter;
    }
    else
    {
        str[i] = currentletter;
    }

modified program :

#include <stdio.h>
#include <string.h>

int main() {
    char palindroom[50],a;
    int lengte, i;
    int woord = 0;

    printf("This program checks if your word is a palindrome.\n");
    printf("Enter your word:\t");
    scanf("%s", palindroom);

    lengte = strlen(palindroom);

    for (i = 0; i < lengte; i++) 
        {
             if (palindroom[i] > 64 && palindroom[i] < 91)
                    { 
                      palindroom[i] = palindroom[i] + 32;
                    }         

        if (palindroom[i] != palindroom[lengte - i - 1]) {
            woord = 1;
            break;
        }
    }

    if (woord) {
        printf("Unfortunately, %s is not palindrome\n\n", palindroom);
    }
    else {
            printf("%s is a palindrome!\n\n", palindroom);
    }
    getchar();
    return 0;
}

65 is the decimal representation of A in the ASCII table, 90 is the decimal representation of Z while a is 97 ( = 65 +32 ) and z is 122 ( = 90 +32 )

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • 1
    What are `91` and other magic numbers? – Weather Vane Jan 07 '18 at 20:54
  • Yes this is excactly what I was looking for. Thank you very much ralf! I knew I had to do something with the ASCII values and this is it. So thank you. – DeDaymar Jan 07 '18 at 21:02
  • edited answer ... – ralf htp Jan 07 '18 at 21:02
  • '64' is A in ASCII , '91' is Z in ASCII and 32 is the difference between a small letter and a capital letter in ASCII. ASCII table represents the values of how the computer sees all the characters and stuff you use – DeDaymar Jan 07 '18 at 21:04
  • ________ nice ... – ralf htp Jan 07 '18 at 21:04
  • 2
    The difference between lower/upper case letters is `'a' - 'A'`. By adding `'a' - 'A'` instead of magic number 32, not only will code's intent be more clear, upper to lower will work in ASCII _and_ EBCDIC. – chux - Reinstate Monica Jan 07 '18 at 23:12
  • @DeDaymar: Weather Vane was being rethorical. There is no reason to use a 'magic' number in C to indicate a character code in the source file's encoding; just use `'a'`. This is (1) more clearer than a seemingly magic value, (2) more compatible in case someone compiles this using another encoding, and (3) more clearer than a seemingly magic value (yes, this repetition is quite deliberate). – Jongware Jan 12 '18 at 10:18