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.
# 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
*/
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
usingnamespace 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;
}
#include <iostream>
#include <cstring>
usingnamespace 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;
}