字符串处理(C++)
生活随笔
收集整理的這篇文章主要介紹了
字符串处理(C++)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
編寫一個函數,處理字符串中的'*',處理規則如下:
1、如果*在字符中間,則刪除;
2、如果*在字符串開始或結束,則保留;
3、如果沒有*或者不是*,則將其他字符輸出。
#include<iostream> #include<string> using namespace std; string delspecchar(string &str) {int len = str.length();int templeft, tempright;for (int i = 0; i < len - 1; i++){if (str[i] != '*'){templeft = i; break;} }for (int j = len - 1; j > 0; j--){if (str[j] != '*'){tempright = j;break;} }if (templeft < tempright-1){for (int n=templeft; n< tempright; n++){if (str[n] == '*'){str.erase(str.begin() + n);tempright -= 1;n = n - 1;} }}return str; } int main() {string str;cout<< "請輸入字符串:"<<endl;cin >> str;cout << delspecchar(str) << endl;system("pause");return 0; }總結
以上是生活随笔為你收集整理的字符串处理(C++)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hutool 读取扩展名文件_批量修改文
- 下一篇: 国际结算重点完整版