这是代码:
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;
}
if (str[i] != '\0')
意思是“如果不在字符串的末尾”
C 字符串(字符数组)中的最后一个字符是空 ( '\0'
) 值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句