1

I have tried to count the numbers in a string but it doesnt work and I think it is logically good. I am a beginner in programming. I know it works for one-digit numbers but that's intentional.

#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int main() 

{
    int numbs [10] = {0,1,2,3,4,5,6,7,8,9};
    string str1;
    cin >> str1;
    vector <unsigned int> positions;

    for (int a = 0 ;a <=10;a++) 
    {
        int f = numbs[a];

        string b = to_string(f);      

        unsigned pos = str1.find(b,0);
        while(pos !=string::npos)
        {
            positions.push_back(pos);
            pos = str1.find(b,pos+1);
            break;
        }
    }

    cout << "The count of numbers:" << positions.size() <<endl;

    return 0;
 }
user3813872
  • 45
  • 1
  • 4

3 Answers3

3

If you need only to count digits in a string then there is no sense to use std::vector. You can count them without the vector. For example

#include <iostream>
#include <string>

int main() 
{
    std::string s( "A12B345C789" );
    size_t count = 0;

    for ( std::string::size_type pos = 0;
          ( pos = s.find_first_of( "0123456789", pos ) ) != std::string::npos;
          ++pos )
    {
        ++count;
    }     

    std::cout << "The count of numbers: " << count << std::endl;

    return 0;
}

The output is

The count of numbers: 8

Also you could use standard algorithm std::count_if defined in header <algorithm>

For example

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

int main() 
{
    std::string s( "A12B345C789" );

    size_t count = std::count_if( s.begin(), s.end(), 
                                  []( char c ) { return std::isdigit( c ); } );

    std::cout << "The count of numbers: " << count << std::endl;

    return 0;
}

The output is

The count of numbers: 8

If you need to count numbers instead of digits in a string then you should use standard C function strtol or C++ function std::stoi

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Although I understand wanting to keep the examples similar, you could note you don't need the lambda wrapper for isdigit in your second example. – toth Jul 07 '14 at 21:07
  • @toth You are wrong. The compiler can encounter a problem of ambiguity because there are several functions with name isdigit. – Vlad from Moscow Jul 07 '14 at 21:11
  • you are correct, apologies. Didn't realize there were template overloads of `std::isdigit`. The only way I could find of disambiguating other than your lambda function was using `static_cast(std::isdigit)`, hardly more readable. – toth Jul 08 '14 at 14:36
0

Use substrings to extract every part of string with a delimiter(normally a space). Then convert each substring to number. The ones that qualify and converts probably are the numbers in your string. See how many you get.

Erfan
  • 349
  • 1
  • 3
  • 15
0

You might also be interested in the C++ function "isdigit":

For example:

include <iostream>
#include <string.h>
#include <vector>
#include <locale>    // std::locale, std::isdigit

using namespace std;

int main() 

{
  // Initialze array with count for each digit, 0 .. 9
  int counts[10] = {0, 0, 0, 0, 0, 0, 0,0, 0, 0 };
  int total = 0;

  // Read input string
  string str;
  cin >> str;

  // Parse each character in the string.  
  std::locale loc;
  for (int i=0; i < str.length(); i++) {
        if isdigit (str[i], loc) {
          int idx = (int)str[i];
          counts[idx]++
          total++;
  }

  // Print results
  cout << "The #/digits found in << str << " is:" << total << endl;   
  // If you wanted, you could also print the total for each digit ...

  return 0;
}
FoggyDay
  • 11,962
  • 4
  • 34
  • 48