-1

The problem I face is the follows:

I have a string which contains information which is in the following fixed information.

club {
level: 210
league: 128
staff: 1451
salary: 3452600
}
club {
level: 211
league: 121
staff: 1451
salary: 3452600
}
... and many more club {...}

I have many entries of club. I want to be able to just extract all the numbers from ALL of the club in a string in the following format.

Desired Output:

2101281451345260021112114513452600

I have the information in the string but I am not able to understand how to efficiently remove the repeating fields from the string such as level:, league:, staff:, club:, salary:, club {}.

I would appreciate any help for a simple algorithm which achieves this.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Angelina
  • 1
  • 3
  • You could use regular expressions to extract just the numbers (as an list). You could then concatenate them if you need a single string – P. Kouvarakis Dec 23 '19 at 22:12
  • @P.Kouvarakis I am very new to c++ and would appreciate if you could give me some help. I have more experience with python. Any help would be very appreciated :) – Angelina Dec 23 '19 at 22:13
  • https://stackoverflow.com/questions/21594533/c-extract-int-from-string-using-stringstream – macroland Dec 23 '19 at 22:14
  • @macroland I do not want to use stringstream. I would prefer to use regex. – Angelina Dec 23 '19 at 22:20
  • Choice is yours; however, the desired output you showed does not necessitate usage of RE. Either stringstream or anatolyg's answer would be a simple approach. – macroland Dec 23 '19 at 22:23
  • You might want to [edit] your question to clarify it. Is there a reason why you want to use `regex`? If yes, please add it to your question, because this will affect the best answer. If there are more things that are important to you, please add them too. – anatolyg Dec 23 '19 at 22:23
  • @anatolyg I do not want to use regex only, I thought it was the easiest way to solve it. I would want the most efficient way of doing it. – Angelina Dec 23 '19 at 22:24
  • Using a combination of `strspn()` and `strnspn()` in a loop would be the most efficient. Regex would be the simplest. – P. Kouvarakis Dec 23 '19 at 22:33

3 Answers3

1

You don't need to treat your numbers as numbers, treating them as characters is good enough.

To check whether a character is a digit, use isdigit:

str = ...;
for (char c: str)
    if (isdigit(c))
        std::cout << c;
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • I would like to extract all of the integers as well.It doesnt answer my question. – Angelina Dec 23 '19 at 22:17
  • @Angelina that is exactly what this code does. It extracts only numeric digits from the string and outputs them, just like you asked for. You can easily save the digits to a `std::string` instead of output them to `std::cout`. Or, do you mean you want to extract the individual whole integers instead? Because that is not what you asked for. – Remy Lebeau Dec 23 '19 at 22:24
  • Oh yesss I was just testing it out and I realised that it is what I does!! I appologize for being unclear. – Angelina Dec 23 '19 at 22:26
  • @RemyLebeau If I want to append it to one string variable instead of printing it, would the .append() method be appropriate? – Angelina Dec 23 '19 at 22:29
  • @Angelina that is one way to do it, yes. Or you can use the `insert()` or `push_back()` method, or the `+=` operator. – Remy Lebeau Dec 23 '19 at 22:34
  • @RemyLebeau I actually used the +=. Thank you so much for the help. Happy coding!! – Angelina Dec 23 '19 at 22:38
1

You could make use of the erase-remove idiom:

#include <algorithm>
#include <string>
#include <cctype>

int main()
{
    std::string input = "club {"\
        "level: 210"\
        "league : 128"\
        "staff : 1451"\
        "salary : 3452600"\
        "}";

    input.erase(std::remove_if(input.begin(), input.end(), [](char c) { return !std::isdigit(c); }),
        input.end());
    //string is now "21012814513452600"
    return 0;
}

This will remove all non-digits from your string.

jignatius
  • 6,304
  • 2
  • 15
  • 30
0

One idea to extract numbers as numbers: you can replace all unneeded characters by spaces, then get the numbers from the string using stringstream:

std::string str = ...;
std::string temp = str; // avoid overwriting the original string
for (char& c: temp) // '&' gives you permission to change characters
    if (!isdigit(c))
        c = ' ';
std::stringstream stream(tmp);
int i;
while (stream >> i)
    std::cout << i; // print it or do whatever else
anatolyg
  • 26,506
  • 9
  • 60
  • 134