欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)字符串刪除字符后逆序輸出

 更新時(shí)間:2020年05月28日 10:03:01   作者:Alex山南水北  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)字符串刪除字符后逆序輸出,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)字符串刪除字符后逆序輸出的具體代碼,供大家參考,具體內(nèi)容如下

輸入若干個(gè)字符串,和一個(gè)英文字符ch。 要求刪除每個(gè)字符串中的字符ch(區(qū)分大小寫),得到新的字符串,然后將新的字符串按照字典逆序排序后輸出
(每個(gè)字符串的長(zhǎng)度不超過30個(gè)字符,字符串總數(shù)不超過30)

輸入:

3
abcddc
sxwcdez
ncvccvd
c

輸出:

sxwdez
nvvd
abdd

C++實(shí)現(xiàn):(適用于初學(xué)者)

#include <iostream>
#include <cstring>

using namespace std;

void sort_array(char s[][30], int n);

void removing(char s[][30], int n, char ch);

int main() {
  int n, i;
  char s[30][30] = {0};
  char ch;
  cout << "input the number of strings:" << endl;
  cin >> n;
  cin.get();//注意要把/n從輸入流中去除
  for (i = 0; i < n; ++i) {
    cout << "input the NO." << i + 1 << " string:" << endl;
    cin.getline(s[i], 30);
  }
  cout << "input ch:" << endl;
  ch = cin.get();
  removing(s, n, ch);
  sort_array(s, n);
  for (i = 0; i < n; ++i) {
    cout << s[i] << endl;//二維字符串?dāng)?shù)組可以通過這種方式直接輸出
  }
  return 0;
}

void sort_array(char s[][30], int n) {
  int i, j, k, temp;
  for (j = 0; j < n - 1; ++j) {
    bool sort_flag = false;
    //相當(dāng)于一個(gè)冒泡排序,這里排序使用了strcmp
    for (i = 0; i < n - j - 1; ++i) {
      char str_temp[30] = {0};
      temp = strcmp(s[i], s[i + 1]);
      if (temp >= 0)continue;
      if (temp < 0) {
        for (k = 0; k < 30; ++k) {
          str_temp[k] = s[i][k];
        }
        for (k = 0; k < 30; ++k) {
          s[i][k] = s[i + 1][k];
        }
        for (k = 0; k < 30; ++k) {
          s[i + 1][k] = str_temp[k];
        }
        sort_flag = true;
      }
    }
    if (!sort_flag) {
      break;
    }
  }
}

void removing(char s[][30], int n, char ch) {
  bool flag = false;
  int i, j, k;
  for (i = 0; i < n; ++i) {
    for (j = 0; s[i][j] != '\0'; ++j) {
      if (s[i][j] == ch) {
        flag = true;
        //去除字符后把后面的往前面移動(dòng)一位
        for (k = j; s[i][k] != '\0'; ++k) {
          s[i][k] = s[i][k + 1];
        }
      } else { flag = false; }
      if (flag) { j -= 1; }
    }
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++實(shí)現(xiàn)含附件的郵件發(fā)送功能

    C++實(shí)現(xiàn)含附件的郵件發(fā)送功能

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)含附件的郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 最新評(píng)論