Page 1 of 1

how to find numbers in text

Posted: Sat Dec 14, 2013 1:14 pm
by Nerfintas
hi guys, i have

Code: Select all

int main()
{
    string text;
    getline(cin, text);
    cout << text;
    return 0;
}
and what i need to do? i need to found how many numbers in text is. for example if i have text "my name is Raimundas 45 Pavilonis 789"; it counts 5 becouse there is five numbers.

Re: how to find numbers in text

Posted: Sat Dec 14, 2013 1:16 pm
by vitinho444
I'm gonna say that the language you're using is c++, i don't know about c++ but there are some languages, like php that have pre-built is_numeric functions.

So what you have to do is see the length of the string, and go char to char and check if(is_numeric) then raise some counter by 1 and you will end up with the number count.

Re: how to find numbers in text

Posted: Sat Dec 14, 2013 1:24 pm
by Nerfintas
amm im new in here so i need exactly code to do that in c++ :)

Re: how to find numbers in text

Posted: Sat Dec 14, 2013 2:46 pm
by vitinho444
I found this piece of code:

Code: Select all

bool is_numeric(char *string)
{
    int sizeOfString = sizeof(string);
    int iteration = 0;
    bool isNumeric = true;

    while(iteration < sizeOfString)
    {
        if(!isdigit(string[iteration]))
        {
            isNumeric = false;
            break;
        }

        iteration++;

    }

    return isNumeric;
}

Re: how to find numbers in text

Posted: Sun Dec 15, 2013 4:24 pm
by Nerfintas
thanks!