C + + realizes reverse output of character string after deleting characters

Time:2020-9-30

This example for you to share the C + + string after deleting the character string output specific code, for your reference, the specific content is as follows

Enter several strings and an English character ch. It is required to delete the character CH (case sensitive) in each string to get a new string, and then output the new string after sorting the new string in reverse order according to the dictionary
(the length of each string shall not exceed 30 characters, and the total number of strings shall not exceed 30)

Input:

3
abcddc
sxwcdez
ncvccvd
c

Output:

sxwdez
nvvd
abdd

C + + implementation: (for beginners)

#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 (); // note that / N is removed from the input stream
  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; // two dimensional string array can be directly output in this way
  }
  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;
    //This is equivalent to a bubble sort, where StrCmp is used
    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] != '
#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 (); // note that / N is removed from the input stream
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; // two dimensional string array can be directly output in this way
}
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;
//This is equivalent to a bubble sort, where StrCmp is used
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;
//After removing the character, move the following one bit forward
for (k = j; s[i][k] != '\0'; ++k) {
s[i][k] = s[i][k + 1];
}
} else { flag = false; }
if (flag) { j -= 1; }
}
}
}
'; ++j) { if (s[i][j] == ch) { flag = true; //After removing the character, move the following one bit forward for (k = j; s[i][k] != '
#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 (); // note that / N is removed from the input stream
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; // two dimensional string array can be directly output in this way
}
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;
//This is equivalent to a bubble sort, where StrCmp is used
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;
//After removing the character, move the following one bit forward
for (k = j; s[i][k] != '\0'; ++k) {
s[i][k] = s[i][k + 1];
}
} else { flag = false; }
if (flag) { j -= 1; }
}
}
}
'; ++k) { s[i][k] = s[i][k + 1]; } } else { flag = false; } if (flag) { j -= 1; } } } }

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Wondertrader releases version v0.5.0

Today (July 28, 2020), wondertrader released the latest version of v0.5.0, along with the latest version of wtpy, v0.5.0 Wondertrader has made the following changes in v0.5.0: High frequency strategy engine officially released。 High frequency strategy engine beforeWonderTraderHowever, due to insufficient testing, it has not been officially released for the time being. It only focuses […]