- 反转字符串
利用双指针不断向中间靠拢, 交换数据
class Solution {
public:
void reverseString(vector
int left = 0;
int right = s.size() - 1;
while(left < right)
{
char tmp = '\0';
tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
};
- 反转字符串II
class Solution {
public:
void reverse(string &s, int left, int right)
{
right--;
while(left < right)
{
char tmp = '\0';
tmp = s[left];
s[left++] = s[right];
s[right--] = tmp;
}
}
string reverseStr(string s, int k)
{
for(int i = 0; i < s.size(); i += 2*k)
{
if(i+k <= s.size())
{
reverse(s, i, i + k);
continue;
}
reverse(s, i, s.size());
}
return s;
}
};
替换数字
新创建了一个字符串
include
using namespace std;
int main()
{
string s;
while(cin >> s)
{
string ret;
for(int i = 0; i < s.size(); ++i)
{
if(s[i] >= 'a' && s[i] <= 'z')
{
ret += s[i];
}
else if(s[i] >= '0' && s[i] <= '9')
{
ret += "number";
}
}
cout << ret << endl;
}
}