0
In this program he only analyzes whether the string has digits, but wanted him to analyze whether it has positive integers. For example:
entree:
a 1 b 12 c 3.14 -20
exit:
a +1 b +12 c 3.14 -20
#include <iostream>
#include <cctype>
#include <string>
#include <sstream>
using namespace std;
bool isdigit(char c)
{
if(c>='0' && c<='9')
return true;
else
return false;
}
int main ()
{
string str;
getline(cin, str);
for (int i=0; i < str.length(); ++i) {
if (isdigit(str[i])==true){
cout << " +" << str[i];
}
if (isdigit(str[i])==false)
cout << str[i];
}
return 0;
}
In this case if I enter the number 12, it returns +1 +2. Any idea?
Have you ever heard of a state machine?
– Woss
not..... what is it...?
– clarawalsh
By its example the program should also treat real numbers and negative integers. It was just a bad example or the definition is not correct?
– anonimo
I don’t understand the question
– clarawalsh
If only to treat "integer and positive numbers" the result should be:
a +1 b +12 c +3. +14 - +20
and nota +1 b +12 c 3.14 -20
. If you want to make a number recognizer (integer or real) search by finite state machine or regular expression.– anonimo
You didn’t mention "space" in your question, are they important? You want to analyze the string between spaces (except the first and last)?
– anonimo
no, I left the spaces just to be able to visualize better
– clarawalsh