版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011544909/article/details/79947755
题目描述
将一个字符中所有出现的数字前后加上符号“*”,其他字符保持不变
public static String MarkNum(String pInStr)
{
return null;
}
输入描述:
输入一个字符串
输出描述:
字符中所有出现的数字前后加上符号“*”,其他字符保持不变
示例1
输入
Jkdi234klowe90a3
输出
Jkdi*234*klowe*90*a*3*
我们可以先对数字统一用两个*号包裹,然后在去掉字符串中出现**的情况。
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
int main()
{
string str;
while(cin>>str)
{
string temp="";
for(int i=0;i<str.size();i++)
{
if(isdigit(str[i])){
temp+='*';
temp+=str[i];
temp+='*';
}
else
temp+=str[i];
}
while(temp.find("**")!=string::npos)
{
auto pos =temp.find("**");
if(pos!=string::npos)
temp=temp.substr(0,pos)+temp.substr(pos+2);
}
cout<<temp<<endl;
}
return 0;
}