2

C语言中如何使用topper和tolower?我尝试运行我制作的程序,它运行正常,问题是因为我应该将它提交到一个网站以检查它是否正确,每次我提交它时,它都会说编译错误。

我在 macbook 上使用 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;
}
4

3 回答 3

17

touppertolower在 中定义ctype.h。只需将此文件包含在#include <ctype.h>.

于 2015-10-06T14:03:28.453 回答
1

您需要包含 header <ctype.h>

int jumlahkata;应该是类型size_t,因为您将结果存储strlen在其中。

或者不要使用它(正如@iharob Sir 也指出的那样),这是不必要的。因为它是 string ,所以只需检查 fornull character作为循环中的条件。

于 2015-10-06T14:06:07.937 回答
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++
于 2015-10-06T14:27:20.100 回答