字符串的調整與替換

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void replacestr(char* str)
{
    if(str == nullptr || strlen(str) == 0)
        return;
    int num = 0;
    int len = 0;
    for(; len < strlen(str) && str[len] != 0; ++len)
    {
        if(str[len] == ' ')
            ++num;
    }
    int j = len + num * 2 - 1;
    for(int i = len - 1; i > -1; --i)
    {
        if(str[i] != ' ')
            str[j--] = str[i];
        else
        {
            str[j--] = '0';
            str[j--] = '2';
            str[j--] = '%';
        }
    }
}
void modify(char* str)
{
    if(str == nullptr || strlen(str) == 0)
        return;
    int j = strlen(str) - 1;
    for(int i = j; i >= 0; --i)
    {
        if(str[i] != '*')
            str[j--] = str[i];
    }
    for(;j > -1;)
    {
        str[j--] = '*';
    }
    return;
}
int main()
{
    char s[20] = "a b   c";
    replacestr(s);
    cout << s << endl;
    char str[] = "12**345";
    modify(str);
    cout << str << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章