Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;函数检查字符是否为大写字母,并计算给定字符串中的大写字母数 我试着写一个C++函数来检查一个字符串中的字符是否是大写字母。_C++ - Fatal编程技术网

C++;函数检查字符是否为大写字母,并计算给定字符串中的大写字母数 我试着写一个C++函数来检查一个字符串中的字符是否是大写字母。

C++;函数检查字符是否为大写字母,并计算给定字符串中的大写字母数 我试着写一个C++函数来检查一个字符串中的字符是否是大写字母。,c++,C++,以下是我的尝试: #include<iostream> #include <conio.h> #include<string> using namespace std; int iscapital(char x) { if (x>='A'&&x<='Z') return 1; else return 0; } main() { char a[20];int len; int c=0; cout<<"ente

以下是我的尝试:

#include<iostream>
#include <conio.h>
#include<string>
using namespace std;
int iscapital(char x)
{
 if (x>='A'&&x<='Z')    return 1;

 else  return 0;
}
main()
{
char a[20];int len; int c=0;
cout<<"enter your line: ";
cin>>a;
len=strlen(a);
for (int i=0;i<=len;i++)
iscapital(a[i]);
if (iscapital)
{
    c++;
}

cout<<"capital letter in string is: "<<c;
}
#包括
#包括
#包括
使用名称空间std;
int iscapital(字符x)
{

如果(x>='A'&&x您没有正确使用
iscapital

for (int i=0;i<=len;i++)
    iscapital(a[i]); // Call the function, ignore the result
if (iscapital)   // <- This is not valid C++
{
    c++;
}

for(int i=0;i您的代码应该如下所示:

int iscapital(char x)
{
       if (x >='A' && x <= 'Z')    return 1;
       else  return 0;
}

int main()
{
  char a[20];int len; int c=0;
  cout<<"enter your line: ";
  cin.getline(a , 20);      
  // Note : ' getline ' will read the entire line written in the console and will stop only at the end line mark...will include and the white spaces .
  // http://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin

  len=strlen(a);
  for (int i = 0;i < len;i++)
  {
    if (iscapital(a[i]))
    {
       c++;
    }
  }
  cout<<"capital letter in string is: "<<c;

  return 0;
 }
intiscapital(字符x)
{
如果(x>='A'&&x更正代码:

  • IsCapital()
    应返回布尔值,而不是整数


  • for(int i=0;i是否需要您自己编写检查代码?如果不使用
    isupper
    ,或者,如果您关心的不是英语ASCII。@FredLarson-C的
    isupper
    也对区域设置敏感。它使用全局区域设置。不应该
    main
    be
    int main()
    ?我也看到你在学习,我强烈建议你学习
    std::string
    ,像
    strlen
    charx[100]
    这样的东西更像是
    C
    的方式,而不是
    C++
    的做事方式。而且,你写的
    if(iscapital)
    我猜你的意思是
    if(iscapital(a[I])
    非常感谢您的帮助,我将查找std::isupper我以前不知道,这很愚蠢…“使用int表示真值或假值已经过时了…”。bool的唯一优势是代码的语义。如果其他开发人员阅读您的代码并看到返回类型的bool,他会立即理解它返回在C++之前,在没有Cux> Boo/<代码>类型的情况下,在C99之前进行了“ReZNICNEN BUGODAN”表示布尔值。在这里有一种情况,即使用<代码> int >代码>代替“代码> BoOL <代码> >来表示C++中函数的布尔结果?也许是时候读一些DOCU了。在C++中,TURBO true表示一个值1(按int形式)/(根据标准),你应该是安全的。C++布尔类型有两个值,true和false,对应的值1和0。C++表示应该有一个int main(int alc,char **)……。应该返回布尔值而不是整数。“。不一定……你们为什么都写了这个?。标准允许使用int返回布尔值,也允许使用bool返回int(强制转换)@AntonVignoli:I不一定,但只要它只返回true或false,那么正确的返回类型是bool,如果存在除2以外的其他值,则为整数ok@IbraOpEd是的,您可以返回1或0,因为编译时它将转换为true或false,但是为了可读性,您应该使用return true或false,而不是return 1或返回0。
    int iscapital(char x)
    {
           if (x >='A' && x <= 'Z')    return 1;
           else  return 0;
    }
    
    int main()
    {
      char a[20];int len; int c=0;
      cout<<"enter your line: ";
      cin.getline(a , 20);      
      // Note : ' getline ' will read the entire line written in the console and will stop only at the end line mark...will include and the white spaces .
      // http://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin
    
      len=strlen(a);
      for (int i = 0;i < len;i++)
      {
        if (iscapital(a[i]))
        {
           c++;
        }
      }
      cout<<"capital letter in string is: "<<c;
    
      return 0;
     }
    
    bool iscapital(char x)
    {
        if (x >= 'A' && x <= 'Z')
            return 1;
        else
            return 0;
    }
    
    main()
    {
        char a[20];
        int len;
        int c = 0;
    
        cout << "enter your line: ";
        cin >> a;
        len = strlen(a);
    
        for(int i = 0; i < len; i++)
        {
            if (iscapital(a[i]))
                c++;
        }
    
        cout << "capital letter in string is: " << c;
    
        return 0;
    }