Search code examples
cc-stringsuppercasestring-literalsfunction-definition

I am writing C function that convert lowercase char to upper case char with using ASCII but Output is not correct


Okay, So I start working on this, I have code below;

+I also have strlen("any string here") func that return len of any str in decimal just keep in your mind.

I take a lover case let's say a, then a will be equal some decimal num in ASCII table then I subtract 32 to get A.

Sadly this is not working, any idea for this? Thank you for all help and your time!

int uppercase(char sent[]) {
    
 for(int i=0; i <= strlen(sent); ++i) {
        if(sent[i]>='a' && sent[i]<='z')
            sent[i] -= 32;
}

Solution

  • This one will work:

    #include <stdio.h>
    #include <string.h>
    
    void uppercase(char sent[]) {
        for (int i = 0; i < (int)strlen(sent); i++) {
            if (sent[i] >= 'a' && sent[i] <= 'z') {
                sent[i] -= 32;
            }
        }
    }
    
    int main(int argc, char* argv[]) {
        if (argc > 1){
            uppercase(argv[1]);
            puts(argv[1]);
        }
    
        return 0;
    }
    

    It compiles without any errors and warnings (using clang), even with options -pedantic -Wall -Wextra.