Harish nawaz
Harish nawaz

Reputation: 1

To split the given strings into integers and characters in C language

For example my input is : 19A70 how can I split it into 19 A 70. By using atoi() func i can only split the first integer 19 and i can't split the other two elements help needed guys!

Upvotes: 0

Views: 88

Answers (4)

Sir Jo Black
Sir Jo Black

Reputation: 2096

I had fun solving this problem. I present my solution here.

The implemented code recognizes numbers of double type and strings parsing an input string.

There is a problem still to solve, when the - is in the middle of the input string and is not followed by a number, but by the decimal separator . the program associates the - to a string.

Warning: The function strtod, used in the code, interpretes as HEX numbers the pieces of string starting with 0x and translates it in decimal numbers.

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

int main(void)
{
    char a[200];
    char b[200];
    char * x, * y;
    int i=0;
    double v=0;

    printf("Hit only <enter> to stop\n\n");
    do {
        printf("Insert a string: ");
        fgets(a,sizeof(a),stdin);
        a[strlen(a)-1]=0; // Removes the \n at the end

            if (a[0]) {
// ----------------- Start parsing   
            x=a;
            do {
                v=strtod(x,&y); // get a number
// Warning strtod translates number starting with 0x from HEX
                if (x!=y)
                    printf("[%f] ",v);

                if (y!=NULL && *y) { 
                    i=0;x=y; // get a string
                    while(*x && !isdigit(*x)
                           && !(*x=='-' && isdigit(*(x+1))) // negative number
                           && !(*x=='.' && isdigit(*(x+1))) // decimal number
                         //&& !(case -.{digit}) is not managed!!
                           )
                        b[i++]=*x++;

                    if (i) {
                        b[i]=0;
                        printf("[%s] ",b);
                    }
                }
            } while(y!=NULL && *y);
        }
        puts("");
// ----------------- End parsing   

    } while(a[0]!=0);

    return 0;
}

Upvotes: 0

chqrlie
chqrlie

Reputation: 144770

You can use strspn() and strcspn() for this:

#include <string.h>

void split_string(const char *s) {
    int len1, len2;
    for (;;) {
        /* count the number of digits */
        len1 = strspn(s, "0123456789");
        if (len1) {
            printf(" %.*s", len1, s);
        }
        s += len1;
        /* count the number of letters */
        len2 = strspn(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        if (len2) {
            printf(" %.*s", len2, s);
        }
        s += len2;
        /* if neither digits nor letters found: stop */
        if (len1 + len2 == 0)
            break;
    }
    printf("\n");
}

Upvotes: 1

"For example my input is : 19A70. How can I split it into 19 A 70."

It is not quite clear to me if you need to parse and split a proper string (as the question title said) or you just want the input to be in different entities according to their type and thereafter print them (as you said you have your input as 19A70).

If it is the first, I`ll delete this answer and use Clifford´s or Charlie´s approach in the other answers.

If it is the latter, just use:

#include <stdio.h>

int main()
{
   int a,c;
   char b;

   if(scanf("%d%c%d",&a,&b,&c) != 3)
   {
       fprintf(stderr,"Error at scanning!");
       return 1;
   }

   printf("%d %c %d",a,b,c);
}

Execution:

/a.out
19A70
19 A 70

Upvotes: 1

Clifford
Clifford

Reputation: 93476

If the format is fixed and guaranteed to be as you have specified, then sscanf() may be appropriate:

char test[] = "19A70" ;
int a = 0 ;
int b = 0 ;
char c ;

int check = sscanf( test, "%d%c%d", &a, &c, &b ) ;    
if( check == 3 )
{
    printf( "%d %d %c\n", a, b, c ) ;
}
else
{
    printf( "Parse failed\n" ) ;
}

sscanf() provides limited scope for validation, but if the data is machine generated or previously validated that is not really an issue.

Upvotes: 0

Related Questions