生活随笔
收集整理的這篇文章主要介紹了
字符串去除特定字符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
輸入字符串s和字符c,要求去掉s中所有的c字符,并輸出結果。
輸入描述:
測試數據有多組,每組輸入字符串s和字符c。
輸出描述:
對于每組輸入,輸出去除c字符后的結果。
#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
;
}
char *getLineMessage() {int preSize
= 2;int len
= preSize
;int totalSize
= preSize
;char *line
= static_cast
<char *>(malloc(preSize
* sizeof(char))), *str
= line
;int c
;if (line
== nullptr
) {return nullptr
;}for (;;) {c
= fgetc(stdin);if (c
== EOF) {break;}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;
}
總結
以上是生活随笔為你收集整理的字符串去除特定字符的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。