1. 程式人生 > >學習筆記 c++ (找出這個字串中首先出現三次的那個英文字元)

學習筆記 c++ (找出這個字串中首先出現三次的那個英文字元)

/*給定一個英文字串,請寫一段程式碼找出這個字串中首先出現三次的那個英文字元。 輸入描述: 輸入資料一個字串,包括字母,數字等。 輸出描述: 輸出首先出現三次的那個英文字元 示例1 輸入 Have you ever gone shopping and 輸出

e */

#include<iostream> #include<string>

using namespace std;

char delect(string a) {     int len = a.size();     for(int i=0; i< len;i++)     {         int count=0;         for(int j=0;j<len;j++)         {             if(a[i] == a[j])             {                 count++;             }             if(count == 3)             {                 return a[i];             }         }             }     return 5; }

int main(int argc, char** argv) {         string a;     cout<<"請輸入字串:";     getline(cin,a);          delect(a);     if(delect(a)==5)     {         cout<<"沒有出現過三次的字元"<<endl;     }     else     {         cout<<"出現三次的是:"<<delect(a)<<endl;     } }