注册 登录
编程论坛 C++教室

多谢

wjffirework 发布于 2015-03-14 19:48, 575 次点击
#include <iostream>
 using namespace std;
 int main()
 {
     char str[100];
     int i=0,a=0,b=0,c=0,d=0;
     cout<<"input a string:  ";
     cin>>str;
     while(str[i]!='\0')
       {
         if(str[i]==' ')
         a++;
         else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
         b++;
         else if(str[i]>='0'&&str[i]<='9')
         c++;
         else
         d++;
         i++;
     }
     cout<<"the number of space is"<<a<<endl;
     cout<<"the number of letter is"<<b<<endl;
     cout<<"the number of figure is"<<c<<endl;
     cout<<"the number of else character"<<d<<endl;
     return 0;
}

[ 本帖最后由 wjffirework 于 2015-3-15 14:35 编辑 ]
8 回复
#2
wjffirework2015-03-14 19:51
我要是改为for循环就可以while就不输出结果
#3
诸葛欧阳2015-03-14 20:44
i没有初始化
#4
wjffirework2015-03-14 22:21
回复 3楼 诸葛欧阳
试过了,给i赋值还是不输出结果
#5
wjffirework2015-03-14 22:26
回复 3楼 诸葛欧阳
刚开始就有试过但是一直好像不跳出那个循环
#6
zcdjt2015-03-14 22:33
你这一题只能验证你输入的第一个数和后面的空格,不知你是不是这样的意思,虽然有点笨。
#include <iostream>
using namespace std;
int main()
{
     char str[100];
     int i,a=0,b=0,c=0,d=0;
     cout<<"input a string:  ";
     cin>>str;
     for(i=0;i<100;i++)
     {
         if(str[i]==' ')
         a++;
         else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
         b++;
         else if(str[i]>='0'&&str[i]<='9')
         c++;
         else
         d++;
         cout<<"input a string:  ";
         cin>>str;
         i++;
     }
     cout<<"the number of space is"<<a<<endl;
     cout<<"the number of letter is"<<b<<endl;
     cout<<"the number of figure is"<<c<<endl;
     cout<<"the number of else character"<<d<<endl;
  system("pause");
  return 0;
}
#7
诸葛欧阳2015-03-14 22:34
程序代码:
#include <iostream>
using namespace std;
int main()
{
     char str[30];
     int i=0,a=0,b=0,c=0,d=0;
     cout<<"input a string:  ";
     cin>>str;
     while(str[i]!='\0')//不要分号
     {
         if(str[i]==' ')
         a++;
         else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
         b++;
         else if(str[i]>='0'&&str[i]<='9')
         c++;
         else
         d++;
         i++;
     }
     cout<<"the number of space is"<<a<<endl;
     cout<<"the number of letter is"<<b<<endl;
     cout<<"the number of figure is"<<c<<endl;
     cout<<"the number of else character"<<d<<endl;
     return 0;
}
#8
wjffirework2015-03-15 09:28
回复 6楼 zcdjt
我那个的意思是输入一串字符,统计其中数字,空格,字母和其他字符的个数输出,我刚开始改为for循环可以,但用while就不输出结果
#9
wjffirework2015-03-15 09:31
回复 7楼 诸葛欧阳
多谢

[ 本帖最后由 wjffirework 于 2015-3-15 10:11 编辑 ]
1