给定一个字符串,找出这个字符串中首先出现三次的那个英文字符,并将其输出。
#include<iostream> #include<cstdlib> #include<string> using namespace std; int main() { string s; while(getline(cin,s)) //获取一行字符串(可以接受空格)并输出,以回车键作为结束符 { int hashtable[256]={0}; for(int i=0;i<s.size();i++) //s.size表示s的大小 { if(isalpha(s[i])) //判断是不是字符 hashtable[s[i]]++; cout<<s[i]<<" "<<hashtable[s[i]]<<" "<<&hashtable[s[i]]<<endl; if(hashtable[s[i]]==3) { cout<<s[i]<<endl; break; } } } return 0; }
调试:
运行结果图:
将这个源程序进行改写,可以改为统计这个字符串中字符出现的次数!