错误~检查字符是否为小写字母

Error ~ Check if character is lowercase letter

这是我编写的用于检查 [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;
     }

我得到的错误是

    ||=== Build: Debug in practice (compiler: GNU GCC Compiler) ===|
    C:\Users\Public\Documents\krish\practice\main.cpp||In function 'int main()':|
    C:\Users\Public\Documents\krish\practice\main.cpp|9|error: conflicting declaration 'char i'|
    C:\Users\Public\Documents\krish\practice\main.cpp|7|note: previous declaration as 'int i'|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

问题正是错误消息所说的:您声明 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;
       }

现在开始工作了~~

一个数组在编译时必须有一个常量大小和一个 non-zero 大小:

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++;
    }