C++ 字符串处理:去除字符串中间的星号
#include <iostream>
#include <string>
int main() {
std::string str;
std::cin >> str;
std::string result;
// 遍历字符串,将前后的*保留,中间的*删除
for (int i = 0; i < str.length(); i++) {
if (i == 0 || i == str.length() - 1 || str[i] != '*') {
result += str[i];
}
}
std::cout << result << std::endl;
return 0;
}
输入描述
一个包含星号的字符串。
输出描述
删除了字符串中除了首尾以外的所有星号的字符串。
用例输入 1
ABC123123abc***********
用例输出 1
ABC123123abc********
原文地址: https://www.cveoy.top/t/topic/pcSV 著作权归作者所有。请勿转载和采集!