Home > Software design >  How to get a number from a string without other symbols
How to get a number from a string without other symbols

Time:10-10

I have a task like that. User inputs 3 numbers in this format (x;y;z). I get them like a string and I need to convert them into 3 numbers. I was trying to do this for a long time now. I need help, because I don't know how to do this.

Input: 22;33;55

Output: Number1 = 22

Number2 = 33

Number3 = 55

The best way to do this would be with string. Because I have Validation in this case.

bool Validation(char input[], int semicolon[])
{
    int is_float = 0;
    int is_semicolon = 0;
    int is_char = 0;
    for(int i = 0; i < strlen(input);   i)
    {
        if(!(input[i] >= '0' && input[i] <= '9'))
        {
            if(input[i] == ';')
            {
                  is_semicolon;
            }else if(input[i] == '.')
            {
            }
            else
            {
                  is_char;
                printf("Neteisingi duomenys.\nBuvo ivestas netinkamas simbolis.\n");
                return false;
            }
        }
    }
    if(is_semicolon > 2 || is_semicolon == 0 || is_semicolon == 1)
    {
        printf("Neteisingi duomenys.\nBuvo neteisingai atskirti duomenys kabliataskiais.\n");
        return false;
    }
    if(is_semicolon == 2)
    {
        for (int i = 0; i < 2;   i) {
            for(int j = 0; j < strlen(input);   j)
            {
                if(input[j] == ';')
                {
                    semicolon[i] = j;
                    printf("Duomenys ivesti teisingai.\n");
                    return true;
                }
            }
        }
    }
}

So if I will scan input as decimal, it wouldn't work for me. What is the simplest way to make it with strtol( )?

CodePudding user response:

Here is the implementation of @MarcoBonelli's idea:

#include <stdio.h>

int main(void) {
    int number[3];
    sscanf("22;33;55", "%d;%d;%d", number, number   1, number   2);
    printf("number 1: %d, number 2: %d, number 3: %d\n", number[0], number[1], number[2]);
}

and the output:

number 1: 22, number 2: 33, number 3: 55

If you want to write a parser it's way more verbose but generic and easily extendable (in this case parsing the gramma: l(;l)* where l is a long and ; your separator). It also illustrates how to use strtol():

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

const char *parse_ch(const char *s, char ch) {
    if(!s || *s != ch)
        return NULL;
    return s 1;
}

const char *parse_long(const char *s, long *l) {
    if(!s)
        return NULL;
    char *endptr;
    *l = strtol(s, &endptr, 10);
    if((*l == LONG_MIN || *l == LONG_MAX) && errno == ERANGE)
        return NULL;
    return endptr;
}

int main(void) {
    const char *s = "22;33;55";
    long l;
    s = parse_long(s, &l);
    if(!s) return 1;
    printf("number: %ld\n", l);
    for(;;) {
        s = parse_ch(s, ';');
        if(!s) return 0;
        s = parse_long(s, &l);
        if(!s) return 1;
        printf("number: %ld\n", l);
    }
}

CodePudding user response:

Simple?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char *s = "42;56;97";

    if( strspn( s, ";0123456789" ) != strlen( s ) ) {
        fprintf( stderr, "Bad data\n" );
        return 1;
    }

    long n1 = strtol(   s, &s, 10 );
    // add check for *s == ';' if you think it appropriate
    long n2 = strtol(   s, &s, 10 );
    // add check for *s == ';' if you think it appropriate
    long n3 = strtol(   s, &s, 10 );
    // add check for *s == '\0' if you think it appropriate

    printf( "%ld  %ld  %ld\n", n1, n2, n3 );

    return 0;
}
42  56  97
  • Related