在 C++ 中查詢兩個字串的不常用字元


在本教程中,我們將討論編寫程式來查詢兩個字串的不常用字元。

為此,將提供兩個字串。我們的任務是打印出兩個字串中未出現的字元,並按順序排列。

例如

 演示

#include <bits/stdc++.h>
using namespace std;
const int LIMIT_CHAR = 26;
//finding the uncommon characters
void calculateUncommonCharacters(string str1, string str2) {
   int isthere[LIMIT_CHAR];
   for (int i=0; i<LIMIT_CHAR; i++)
      isthere[i] = 0;
      int l1 = str1.size();
      int l2 = str2.size();
   for (int i=0; i<l1; i++)
      isthere[str1[i] - 'a'] = 1;
   for (int i=0; i<l2; i++) {
      if (isthere[str2[i] - 'a'] == 1 || isthere[str2[i] - 'a'] == -1)
         isthere[str2[i] - 'a'] = -1;
      else
         isthere[str2[i] - 'a'] = 2;
   }
   for (int i=0; i<LIMIT_CHAR; i++)
      if (isthere[i] == 1 || isthere[i] == 2 )
         cout << (char(i + 'a')) << " ";
}
int main() {
   string str1 = "tutorials";
   string str2 = "point";
   calculateUncommonCharacters(str1, str2);
   return 0;
}

輸出

a l n p r s u

更新於:19-Aug-2020

113 次瀏覽

開啟你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.