统计字符..请高手改程序!!!
请把下面的程序改掉由键盘输入各种字符中文字体英文字母,数字等字符.
没有限制你要输入多少个.当碰到"~"这个字符的就停止输入.
然后统计
英文字母有几个按你输入的顺序输出,
中文字有几个按你输入的顺序输出.
数字有几个按你输入的顺序输出.
其他字符有多少按你输入的顺序输出.
空格有几个
请回帖的大哥们看清楚问题后
- C/C++ code
#include <iostream>
using namespace std;
int main()
{
int charcount = 0, chinesecount = 0, numcount = 0, spacecount = 0, othercount = 0, totalcount;
cout < <"请输入一行字符:";
char pstr[] = "1 a ,,,我 你", curchar;
// cin>>pstr;
curchar = pstr[0];
int curindex = 1;
while(curchar != '\0')
{
if (curchar >= 65 && curchar <= 122) //A-z
charcount++;
else if (curchar >= 128 || curchar < 0) //中文
chinesecount++, curindex++; //中文字符占两个字节,因此将游标向后移一位
else if (curchar >= 48 && curchar <= 57) //0-9
numcount++;
else if (curchar == 32) //空格
spacecount++;
else
othercount++;
curchar = pstr[curindex++];
}
totalcount = charcount + chinesecount + numcount + spacecount + othercount;
cout < <"总数:" < <totalcount < <" 英文字符:" < <charcount < <" 中文字符" < <chinesecount
< <" 数字:" < <numcount < <" 空格:" < <spacecount < <" 其它:" < <othercount;
system("pause");
return 0;
}
[解决办法]
- C/C++ code
#include<iostream>#include <string>using namespace std;int main() { int charcount = 0, numcount = 0, spacecount = 0, othercount = 0, totalcount; string strChar, strNum, strSpace, strOther; cout<<"请输入一行字符:"; char curchar; int curindex = 1; while(cin.get(curchar) && curchar != '~') { if (curchar >= 65 && curchar <= 122) //A-z { strChar += curchar; charcount++; } else if (curchar >= 48 && curchar <= 57) //0-9 { strNum += curchar; numcount++; } else if (curchar == 32) //空格 { strSpace += curchar; spacecount++; } else { strOther += curchar; othercount++; } } totalcount = charcount + numcount + spacecount + othercount; cout<<"总数:" << totalcount << endl <<" 英文字符:"<<charcount << "内容:" << strChar << endl <<" 数字:" << numcount << "内容:" << strNum << endl <<" 空格:" << spacecount << "内容:" << strSpace << endl <<" 其它:"<<othercount << "内容:" << strOther << endl; system("pause"); return 0;}