题目:删除字符串中的数字并压缩字符串。

举例:输入字符串"abc123de4fg56", 输出"abcdefg"。

要求:不开辟新空间,时间复杂度为O(n)。

答:

#include "stdafx.h"
#include <iostream>

using namespace std;

//删除字符串中的数字并压缩字符串
void RemoveNumberChar(char *str)
{
    if (NULL == str)
    {
        return;
    }
    char *p = str;
    char *pNewStr = str;
    while (*p != '\0')
    {
        if (*p < '0' || *p > '9')
        {
            *pNewStr = *p;
            pNewStr++;
        }
        p++;
    }
    *pNewStr = '\0';
}

int _tmain(int argc, _TCHAR* argv[])
{
    char str[] = "abc123de4fg56";
    RemoveNumberChar(str);
    cout<<str<<endl;
    return 0;
}

运行界面如下:

删除字符串中的数字并压缩字符串

相关文章:

  • 2021-09-01
  • 2021-09-17
  • 2021-10-25
  • 2021-09-23
猜你喜欢
  • 2021-11-27
  • 2021-12-15
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
相关资源
相似解决方案