如何统计字符串中有多少个、何种类型的数字并用空格替换所有字母?

问题描述 投票:0回答:2

我需要使用库字符串函数或不使用它们来解决此任务。

我在没有特殊功能的情况下解决了:

void without_functions(string str)
{
    int* how_many_num = new int[10];
    for (int i = 0; i < 10; ++i)
        how_many_num[i] = 0;

    for (int i = 0; i < str.size(); ++i)
    {
        if (str[i] >= '0' && str[i] <= '9')
        {
            ++how_many_num[int(str[i]) - 48];
        }
    }

    for (int i = 0; i <= 9; ++i)
    {
        if (how_many_num[i] != 0)
        {
            cout << "Digit " << i << " is founded" << how_many_num[i] << " times" << endl;
        }
    }
   
    for (int i = 0; i < str.size(); ++i)
    {
        if ((int(str[i]) >= 65 && int(str[i]) <= 90) || (int(str[i]) >= 97 && str[i] <= '122'))
        {
            str[i] = ' ';
        }
    }

    cout << endl << "New string:  " << str << endl;
}

我想不出如何用字符串函数(方法)来实现这个任务。

c++ string char
2个回答
0
投票

without_functions()
有很多问题:

  • 漏水了

    how_many_num
    。使用完后,您需要
    delete[]
    它。最好用
    std::vector
    代替,但是对于这么小的数组,使用动态内存没有意义,就用固定数组代替吧

  • 您的

    cout
    循环不允许使用
    0
    数字。您应该使用
    0..9
    以外的初始值填充数组元素,然后查找该值而不是
    0

  • 您的替换循环正在寻找

    '122'
    ,而它应该寻找
    122
    。然而,您确实应该使用字符文字而不是数字 ASCII 代码。在编码中使用幻数是一个坏习惯。

  • 用于计算数字和替换字母的循环可以合并为一个循环。

现在,尝试更多类似这样的事情:

void without_functions(string str)
{
    int how_many_num[10];
    for (int i = 0; i < 10; ++i)
        how_many_num[i] = -1;

    for (size_t i = 0; i < str.size(); ++i)
    {
        if (str[i] >= '0' && str[i] <= '9')
        {
            ++how_many_num[str[i] - '0'];
        }
        else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
        {
            str[i] = ' ';
        }
    }

    for (int i = 0; i < 10; ++i)
    {
        if (how_many_num[i] != -1)
        {
            cout << "Digit " << i << " was found " << how_many_num[i] << " times" << endl;
        }
    }
   
    cout << endl << "New string:" << str << endl;
}

现在,要将其转换为标准库函数,请尝试如下操作:

#include <array>
#include <string>
#include <cctype>
...

void with_functions(string str)
{
    array<int, 10> how_many_num;
    how_many_num.fill(-1);

    for (char &ch : str)
    {
        if (isdigit(ch))
        {
            ++how_many_num[ch - '0'];
        }
        else if (isalpha(ch))
        {
            ch = ' ';
        }
    }

    for (int i = 0; i < 10; ++i)
    {
        if (how_many_num[i] != -1)
        {
            cout << "Digit " << i << " is found " << how_many_num[i] << " times" << endl;
        }
    }
   
    cout << endl << "New string: " << str << endl;
}

或者:

#include <array>
#include <string>
#include <map>
#include <cctype>
...

void with_functions(string str)
{
    map<char, int> how_many_num;

    for (char &ch : str)
    {
        if (isdigit(ch))
        {
            ++how_many_num[ch];
        }
        else if (isalpha(ch))
        {
            ch = ' ';
        }
    }

    for (const auto &elem : how_many_num)
    {
        cout << "Digit " << elem.first << " is found " << elem.second << " times" << endl;
    }
   
    cout << endl << "New string: " << str << endl;
}

0
投票

一些用户在使用“使用库字符串函数”这一措辞时可能会感到困惑。 我只是假设您指的是标准库函数。

有几种方法可以实现这一目标:

  1. std::replace_if
    std::isdigit
  2. 正则表达式替换
  3. 较新的范围取代了
  4. 您还可以使用字符串直接
    replace
    函数 - 但我不建议将其用于此类练习。

选择你最喜欢的,我建议学习所有的。

© www.soinside.com 2019 - 2024. All rights reserved.