How to see if a string has integer numbers

Asked

Viewed 69 times

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?

  • not..... what is it...?

  • 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?

  • I don’t understand the question

  • 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.

  • You didn’t mention "space" in your question, are they important? You want to analyze the string between spaces (except the first and last)?

  • no, I left the spaces just to be able to visualize better

Show 2 more comments

2 answers

0

There is already an isdigit function in the ctype library, no need to implement a.

Add a while to print all consecutive digits:

for (int i=0; i < str.length(); ++i)
     if (isdigit(str[i])==true){
            cout << " +";
            while(i < str.length() && isdigit(str[i])) 
               cout << str[i++];
     }
     else
        cout << str[i];

0

To have a super robust solution will need complex logic, but to solve the cases indicated in the question does not need much.

With 2 more variables and changing only in main can do:

int main () {
    string str, num = "";
    getline(cin, str);
    char ultima_letra = 0; //para saber se o numero tem um - ou . antes 

    for (size_t i=0; i < str.length(); ++i) {
        if (isdigit(str[i])) {
            num += str[i]; //junta os dígitos para o número
        } else {
            if (num.length() > 0 && str[i] != '.' && ultima_letra != '.' && ultima_letra !='-'){
                cout << "+";
            }
            ultima_letra = str[i];
            cout << num << str[i];
            num = ""; //escreveu o numero por isso tem de limpar
        }
    }
    if (num.length() > 0 && ultima_letra != '.' && ultima_letra !='-'){
        cout << "+";
    }
    cout << num << endl;

    return 0;
}

The example you mentioned working on Ideone

The logic is to store the integer number that you pick up in a string, concatenating digit by digit, then when you pick a letter you check if it is a positive integer by referring to the letter before and after the number. If it is a . before or after, and if it is a - before discards as number. In all other cases consider as number and write the + before writing the number.

  • Aaah thank you so much! I get it!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.