Home > OS >  How to count how many and what kind of digits are found in a string and replace all letters with spa
How to count how many and what kind of digits are found in a string and replace all letters with spa

Time:11-01

I need to solve this task using library string functions and without them.

I solved without special functions:

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;
}

I cannot come up with how to implement this task with string functions (methods).

CodePudding user response:

Welcome to Stackoverflow!

Some users might find it confusing when using phrasing "using library string functions". I only assume you meant standard library functions.

There are couple of ways to achieve this:

  1. std::replace_ifand std::isdigit
  2. regex replace
  3. The newer ranges replace
  4. You can also use string direct replace function - but I don't recommend it for this kind of exercise.

Choose your favorite, I'd recommend learning all of them :) Best of luck!

CodePudding user response:

There are a number of problems with without_functions():

  • it is leaking how_many_num. You need to delete[] it when done using it. Better to use std::vector instead, but for such a small array, there is no point in using dynamic memory, just use a fixed array instead

  • your cout loop is not allowing for 0 digits. You should fill the array elements with an initial value other than 0..9, and then look for that value instead of 0.

  • your replacement loop is looking for '122' when it should be looking for 122 instead. However, you really should be using character literals instead of numeric ASCII codes. It is a bad habit to use magic numbers in coding.

  • your loops to count digits and replace letters can be combined into a single loop.

Now, try something more like this:

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;
}

Now, to translate that into standard library functions, try something like this:

#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;
}

Alternatively:

#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;
}
  • Related