toupper tolower

纵饮孤独 提交于 2019-12-11 10:19:45

问题


How to 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 since I should submit it to a website to check it whether it's right or wrong, every time I submit it, it says compile error.

I made the code on macbook, using Xcode and it says on 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;
}

回答1:


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




回答2:


You need to include header <ctype.h> .

Also int jumlahkata; should be of type size_t as you store result of strlen in it.

Or don't use it (as also pointed out by @iharob Sir ) , it unnecessary. As it is string , just check for null character as a condition in loop.




回答3:


You are mixing C with C++:

int input;
scanf("%d",&input);          // in C, following the first executable statement you may not declare variables until the next block
int jumlahkata;              // declaring the variable here is C++

char kalimat[100];           // declaring the variable here is C++

for(int i=0;i<input;i++)     // declaring the variable here is C++


来源:https://stackoverflow.com/questions/32971979/toupper-tolower

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!