[Solved] split char* into two char* by a char at the first position of the char


Catch!:)

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


int main(void) 
{
    char *version = "10.5.108";
    char *first, *second;

    if ( ( second = strchr( version, '.' ) ) == NULL )
    {
        first = malloc( strlen( version ) + 1 );
        strcpy( first, version );
        second = malloc( sizeof( char ) );
        *second = '\0';
    }
    else
    {
        ptrdiff_t n = second - version;
        first = malloc( ( n + 1 ) * sizeof( char ) );
        second = malloc ( ( strlen( version ) - n ) * sizeof( char ) );

        strncpy( first, version, n );
        first[n] = '\0';
        strcpy( second, version + n + 1 );
    }

    printf( "|%s|%s|", first, second );

    free( first );
    free( second );

    return 0;
}

The output is

|10|5.108|

solved split char* into two char* by a char at the first position of the char