Convert lower case letter to upper case without using "toupper()"

Asked

Viewed 128 times

1

I am working on a function to turn in capital letters, but my retouching is strange. In the end is giving the result below:

Word input data:

lucas 

Program is returning:

LUCASÓÓÓÓÓÉð
    #include <stdio.h>
    #include <stdlib.h>

    char convmaiuscula (char texto [10]){
    int i,tam;
     for (i=0;i < strlen(tam);i++)
     {
          texto[i] = texto[i]-32;
     }
     printf("A palavra em maiusculas %s  " ,texto);
     }

     int main()
     {
     char palavra [10];
     printf ("\nDigite uma palavra: ");
     gets (palavra);
     convmaiuscula(palavra);
     }

1 answer

4


There are several errors there, not to mention the bad organization and nomenclature and the use of things that are not necessary.

Already in the main code if you do not reserve the space for the string will corrupt memory, and the worst that can happen is it works, as it happens in several cases, and you will find that you are right.

To request data use scanf() or fgets(), never gets(). Read How to read from stdin in C?.

To verify the end of the string see if you found the terminator. Read Whenever I am going to scan a string in C I must use "strlen()", right?.

That one tam doesn’t make any sense, it seems a random code that was placed there.

I did not fix the error of not converting right every range of characters that can be typed because this is a conceptual error, just as the function says it will convert something and ends up printing this something too.

I changed the function return to compile correctly since there is no return. If I was going to get a return I’d have to encode that and probably take out the printf(), but it makes no sense to return if the conversion is made inplace in the original.

#include <stdio.h>

void convmaiuscula(char texto[11]) {
    for (int i = 0; texto[i] != '\0'; i++) texto[i] -= 32;
    printf("A palavra em maiusculas %s", texto);
}

int main() {
    char palavra[11];
    printf("Digite uma palavra: ");
    scanf("%s10", palavra);
    convmaiuscula(palavra);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thanks for the feedback Maniero, regarding the organization, ordering and indentation of the code I believe it was due to lack of experience to the use of stackoverflow when posting the same code to what is in codeblock. I will pay more attention to this in the next publications. Thank you

  • @ Maniero, I ended up adding in the code a condition where the user corrects the problem of putting a capital letter together in the plavra. ' void convmaiuscula(char text[11]) { for (int i = 0; text[i] != ' 0'; i++) if ((text[i] >= 97) && (text[i] <= 122)){ text[i] -= 32; } printf("Upper case word %s", text); } int main() { char word[11]; printf("Type a word: "); scanf("%S10", word); convuscula(word); } '

  • It’s a good call.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.