[英]What does str[i]!='\0' mean?
This is the code:这是代码:
for (int i = 0; str[i]!='\0'; ++i)
This is the full code I got online:这是我上网的完整代码:
#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 is zero character. \0 是零字符。 In C++ it is used to indicate the termination of a character string
在 C++ 中用于表示字符串的终止
if (str[i] != '\0')
means "if not at end of the string" if (str[i] != '\0')
表示“如果不在字符串末尾”
The last char in a C string (char array) is a null ( '\0'
) value. C 字符串(字符数组)中的最后一个字符是 null (
'\0'
) 值。
What does str[i]?='\0' mean?
str[i]?='\0' 是什么意思?
!=
is the inequality operator. !=
是不等式运算符。 It yields false when the operands are equal and true otherwise.当操作数相等时它产生假,否则产生真。
str[i]
and '\0'
are the operands in this expression. str[i]
和'\0'
是此表达式中的操作数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.