如何在 C 中使用 toupper 和 tolower?

问题描述 投票:0回答:3

如何在C语言中使用

topper
tolower

我尝试运行我编写的程序,它运行正常。问题是,当我提交到网站检查是否正确时,显示编译错误。

Xcode 在我的 toupper 和 tolower 代码中显示此错误:

函数“toupper”的隐式声明在 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;
}
c tolower toupper
3个回答
19
投票

toupper
tolower
ctype.h
中定义。只需将此文件包含在行
#include <ctype.h>
中即可。


1
投票

您需要包含标题

<ctype.h>

此外,当您将

int jumlahkata;
的结果存储在其中时,
size_t
应该是
strlen
类型。

或者不要使用它(正如@iharob Sir也指出的那样),这是不必要的。由于它是 string ,只需检查

null character
作为循环中的条件即可。


0
投票

您正在将 C 与 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++
© www.soinside.com 2019 - 2024. All rights reserved.