C++ 错误~检查字符是否为小写字母
这是我用来检查[I]是否是小写字母的代码C++ 错误~检查字符是否为小写字母,c++,lowercase,C++,Lowercase,这是我用来检查[I]是否是小写字母的代码 #include<iostream> #include<conio.h> #include<stdio.h> using namespace std; int main() { int i=0; char str[i]="Enter an alphabet:"; char i; while(str[i])
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
int i=0;
char str[i]="Enter an alphabet:";
char i;
while(str[i])
{
i=str[i];
if (islower(i)) i=toupper(i);
putchar(i);
i++;
}
return 0;
}
问题正是错误消息所说的:您声明了两次
i
。一次作为char
,一次作为int
int i=0; // declare i as int and assign 0
char str[i]="Enter an alphabet:";
char i; // declare i as char -> i is already declared as int.
重命名一个变量
也不要使用conio.h
——它不是标准C库的一部分,也不是POSIX定义的。新代码~~~
/* islower example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="Test String.\n";
char c;
while (str[i])
{
c=str[i];
if (islower(c)) c=toupper(c);
putchar (c);
i++;
}
return 0;
}
/*岛下示例*/
#包括
#包括
int main()
{
int i=0;
char str[]=“测试字符串。\n”;
字符c;
while(str[i])
{
c=str[i];
如果(islower(c))c=toupper(c);
普查尔(c);
i++;
}
返回0;
}
现在它的工作状态~~一个数组在编译时必须有一个恒定的大小和一个非零的大小:
int i = 0;
char str[i] = "Enter an alphabet:"; //
在您的代码中,上面的i
必须是常量,不能是0
所以你可以这样声明:
const int SIZE = 50;
char str[SIZE] = "Enter an alphabet:";
这里也有:
char i;
while(str[i])
上面您使用了i
而没有初始化它,并且使用了char
作为数组的索引
您的代码如下所示:
const int SIZE = 50; // constant size
char str[SIZE] = "Enter an alphabet:";
//if you want : char str[] = "Enter an alphabet:";
int i = 0; // initialize
while( i < strlen(str))
{
char c = str[i];
if(islower(c))
c = toupper(c);
putchar(c);
i++;
}
const int SIZE=50;//恒定大小
char str[SIZE]=“输入字母表:”;
//如果需要:char str[]=“输入字母表:”;
int i=0;//初始化
而(i
在一个不相关的注释中,您实际上不需要islower
检查。如果参数不是小写字母,则返回未修改的参数。以后,请尝试实际读取错误消息。消息非常清楚。C++的哪一个版本允许零大小数组?
const int SIZE = 50; // constant size
char str[SIZE] = "Enter an alphabet:";
//if you want : char str[] = "Enter an alphabet:";
int i = 0; // initialize
while( i < strlen(str))
{
char c = str[i];
if(islower(c))
c = toupper(c);
putchar(c);
i++;
}