how to find numbers in text

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Nerfintas
Posts: 54
Joined: Wed Aug 28, 2013 6:01 pm

how to find numbers in text

Post 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.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: how to find numbers in text

Post 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.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Nerfintas
Posts: 54
Joined: Wed Aug 28, 2013 6:01 pm

Re: how to find numbers in text

Post by Nerfintas »

amm im new in here so i need exactly code to do that in c++ :)
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: how to find numbers in text

Post 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;
}
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Nerfintas
Posts: 54
Joined: Wed Aug 28, 2013 6:01 pm

Re: how to find numbers in text

Post by Nerfintas »

thanks!
Post Reply

Return to “Coding”