日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

字符串去除特定字符

發(fā)布時(shí)間:2024/4/11 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串去除特定字符 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

輸入字符串s和字符c,要求去掉s中所有的c字符,并輸出結(jié)果。

輸入描述:

測試數(shù)據(jù)有多組,每組輸入字符串s和字符c。

輸出描述:

對(duì)于每組輸入,輸出去除c字符后的結(jié)果。

  • 示例1
    • 輸入
      heallo
      a
    • 輸出
      hello
#include <iostream> #include <fstream> #include <string> #include <ostream> #include <iterator> #include <vector> #include <algorithm> #include <map> #include <queue> #include <unordered_set> #include "cstring"#define N 1001 using namespace std;int getStringLength(const char array[]) {int length = 0;if (array == nullptr) {return length;}while (array[length] != '\0') {length++;}return length; }/*** 鍵盤輸入一行數(shù)據(jù)* @return*/ char *getLineMessage() {// 預(yù)先準(zhǔn)備的大小int preSize = 2;int len = preSize;int totalSize = preSize;char *line = static_cast<char *>(malloc(preSize * sizeof(char))), *str = line;int c;// 申請內(nèi)存失敗if (line == nullptr) {return nullptr;}for (;;) {c = fgetc(stdin);// 輸入是終止if (c == EOF) {break;}//判斷是否已經(jīng)用盡空間if (--len == 0) {len = totalSize;totalSize = totalSize * 2;char *newLine = static_cast<char *>(realloc(str, totalSize));if (newLine == nullptr) {free(str);return nullptr;}// 指針后移line = newLine + (line - str);str = newLine;}if ('\n' == (*line++ = c)) {break;}}*(--line) = '\0';return str; }int main() {char *message = getLineMessage();char target;cin >> target;int length = strlen(message);int j = 0;for (int i = 0; i < length; i++) {if (message[i] != target) {message[j++] = message[i];}}message[j] = '\0';cout << message << endl;return 0; // nwlrmqhcdarzowkkyhiddqscdxrjmowfrxsjyldefsarcynecdyggxxpklorellnmpapqfwkhopkmcoqhnwnkuewhsqmg // nwlrmqhcdarzowkkyhiddqscdxrjmowfrxsjyldefsarcynecdyggxxpklorellnmpapqfwkhopkmcoqhnwnkuewhsqmg}

總結(jié)

以上是生活随笔為你收集整理的字符串去除特定字符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。