Search code examples
ctolowertoupper

How do I use toupper and tolower in C?


How do I use topper and tolower in the C language?

I've tried to run the program that I've made, it runs properly. The problem is that, when submitting it to a website to check it whether it's right, it shows that I have a compile error.

Xcode shows this error for my toupper and tolower code:

implicit declaration of function 'toupper' is invalid in C99

#include <stdio.h>
#include <string.h>
int main()
{
    int input;
    scanf("%d",&input);
    int jumlahkata;
    
    char kalimat[100];

    for(int i=0;i<input;i++)
    {
        scanf("%s",kalimat);
        jumlahkata=strlen(kalimat);
        for(int j=0;j<jumlahkata;j++)
        {
            if(j%2==0 || j==0)
            {
                kalimat[j]=toupper(kalimat[j]);
            }
            else
            {
                kalimat[j]=tolower(kalimat[j]);
            }
        }
        printf("%s\n",kalimat);
    }
    
    return 0;
}

Solution

  • toupper and tolower are defined in ctype.h. Simply include this file with the line #include <ctype.h>.