Extracting and adding integers in a string.

The program must extract the integers between two string characters and return the sum of them.
EX: iam12aninquis875ite1learn100er.
output: 988
explanation: 12+875+1+100=988.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
int main()
{
 int i,arrsize,integer,sum=0;
 char arr[9999];
 fgets(arr,9999,stdin);
 arrsize=strlen(arr)-1;
 for(i=0;i<arrsize;i++)
{
 //if(isdigit(arr[i]))
 {
 //
 }
 sum=sum+integer;
}
 printf("%d",sum);

}


i need to know how to take the integer as a whole eg.100 and not '1' '0' '0'.
wherein i get 25 as the output which is incorrect.
Use a string.

Loop through the string changing all non-digit characters into whitespace.

stringstream the result into successive integers, adding them to the sum as you do so.

using std::sregex_iterator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# include <iostream>
# include <regex>
# include <string>
# include <sstream>

int main()
{
    std::regex re("[0-9]+");
    std::string line = "iam12aninquis875ite1learn100er";

    auto search_begin = std::sregex_iterator(line.begin(), line.end(), re);
    auto search_end = std::sregex_iterator();
    int sum{};

    for (auto itr = search_begin; itr != search_end; ++itr)
    {
        std::istringstream stream{(*itr).str()};
        int extract{};
        stream >> extract;
        if (!stream.fail()) sum += extract;
    }
     std::cout << sum << "\n";
}
/*useful references:
http://www.cplusplus.com/reference/regex/ECMAScript/
http://en.cppreference.com/w/cpp/regex/regex_iterator
*/
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <string.h>

#include <stdlib.h>

int main ()
{
    char delimiters[] = "abcdefghijklmnopqrstuvwxyz";
    
    char str[] ="iam12aninquis875ite1learn100er";
    char * pch;
    
    pch = strtok (str,delimiters);
    
    int number = atoi(pch);
    int total = 0;
    
    while (pch != NULL)
    {
        number = atoi(pch);
        total += number;
        
        printf ("%d\n",number);
        
        pch = strtok (NULL, delimiters);
    }
    
    printf("Total: %d \n", total);
    
    return 0;
}

12
875
1
100
Total: 988 
Program ended with exit code: 0
To reduce the # of std::istringstream objects used in the program, slight re-jig to the above post:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# include <iostream>
# include <regex>
# include <string>
# include <sstream>

int main()
{
    std::regex re("[0-9]+");
    std::string line = "iam12aninquis875ite1learn100er";

    auto search_begin = std::sregex_iterator(line.begin(), line.end(), re);
    auto search_end = std::sregex_iterator();
    std::string numberString{};

    for (auto itr = search_begin; itr != search_end; ++itr)
    {
        numberString.append(std::move((*itr).str()) + std::string{" "});
    }
     std::istringstream stream{numberString};
     int sum{}, extract{};
     while (stream >> extract)
     {
         if(stream) sum += extract;
     }
     std::cout << sum << "\n";
}
/*useful references:
http://www.cplusplus.com/reference/regex/ECMAScript/
http://en.cppreference.com/w/cpp/regex/regex_iterator
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <regex>
#include <string>
#include <numeric>

int main()
{
    // integer: optional +/-sign, followed by one or more decimal digits
    const std::regex integer_re( "[+-]?\\d+" );

    // 123 + -2345 + 789 + -567 == -2000
    const std::string str = "-this123str+ing-2345+cont-ains+789embedded-567integers+";

    const std::sregex_iterator begin { str.begin(), str.end(), integer_re }, end ;
    const auto plus = [] ( long long a, const auto& b ) { return a + std::stoll( b.str() ) ; };

    try { std::cout << std::accumulate( begin, end, 0LL, plus ) << "\n"; } // -2000
    catch( const std::out_of_range& ) { std::cout << "encountered an out of range integer value\n" ; }
}

http://rextester.com/XPRIX76094
http://rextester.com/ODWC38094
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
   string s = "iam12aninquis875ite1learn100er";
   for ( char& c : s ) if ( !isdigit( c ) ) c = ' ';
   stringstream ss( s );
   int n, sum = 0;
   while( ss >> n ) sum += n;
   cout << sum;
}



Or you could go back to basics:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   char s[9999] = "iam12aninquis875ite1learn100er";

   int n = 0, sum = 0;
   for ( int i = 0; i < strlen( s ); i++ )
   { 
      int d = s[i] - '0';
      if ( d >= 0 && d <= 9 )
      {
         n = 10 * n + d;
      }
      else
      {
         sum += n;
         n = 0;
      }
   }
   sum += n;
   cout << sum;
}
Topic archived. No new replies allowed.