簡體   English   中英

str[i]?='\0' 是什么意思?

[英]What does str[i]!='\0' mean?

這是代碼:

for (int i = 0; str[i]!='\0'; ++i)

這是我上網的完整代碼:

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
    char str[50];
    int v = 0, c = 0, n = 0, s = 0;
    cout << "Enter a string : ";
    gets(str);

//The line
    for (int i = 0; str[i]!='\0'; ++i)
    {
        if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')
        {
            ++v;
        }
        else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
        {
            ++c;
        }
        else if (str[i] >= '0' && str[i] <= '9')
        {
            ++n;
        }
        else
        {
            ++s;
        }
    }
    cout << "Number of vowels : " << v;
    cout << "\nNumber of consonants : " << c;
    cout << "\nNumber of numbers :" << n;
    cout << "\nNumber of special characters : " << s;
    return 0;
}

\0 是零字符。 在 C++ 中用於表示字符串的終止

if (str[i] != '\0')表示“如果不在字符串末尾”

C 字符串(字符數組)中的最后一個字符是 null ( '\0' ) 值。

str[i]?='\0' 是什么意思?

!=是不等式運算符。 當操作數相等時它產生假,否則產生真。 str[i]'\0'是此表達式中的操作數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM