简体   繁体   中英

Building an array of strings from user input

I have the following simple program that will take user string input (" ") from the terminal and parse the input into an array of strings (called array) , character by character. Whenever a numeric character is encountered a new string is created: ie ./programname "Hello1 my name is 2john" would output as:

Hello

1 my name is 

2john

So, I am checking each character of the input and then deciding whether to strcat it to the current string at array[j] or allocate another string array[j+1] and append it to the beginning of that.

The code is as follows:

/*concatenates ts[i] and array[j]*/
char *add_to(char * copy, char ** array, int i , int j,int current_size){
    /* expand size of string at array[j]*/
    array[j] = realloc(array[j],sizeof(char)*(current_size+2));
    /*concate next character of ts*/
    char * target = malloc(sizeof(char)*2);
    strncpy(target,copy+i,1);
    target=strcat(target,"\0");
    array[j]=strcat(array[j],target);
    return array[j];

}

int main(int argc, char** argv){

    /*will hold literals*/
    char ** array = malloc(sizeof(char*)*1);
    /* current size */
    int array_size =1 ;
    /* terminal input */
    char * ts = *(argv+1);

    /* index of array */
    int j =0;
    /* index of terminal input */
    int i =0;
    /*current place to concatenate to in array[j] */
    int current_size=0;
    /* while there is more input */
    while(i<strlen(ts)){
    /* if we need more space */
        if(j==array_size){
            printf("%s\n","Expand");
            array_size++;
            /* assign old pointer to new pointer */
            array=realloc(array,array_size*sizeof(char*));
        }
        /* concatenate array[j] and character at ts[i]*/
        array[j]=add_to(ts,array,i,j,current_size);
        /*new spot to concatenate when loop around */
        current_size++;
        /* move onto next character of ts */
        i++;
        /*if this new character is a digit move to next slot of array */
        if(i<strlen(ts)&&isdigit(*(ts+i))!=0){
            j++;
            current_size=0;
        }   
    }
    /* print all literals in array */
    j=0;
    while(j<array_size){
        printf("%s\n",array[j]);
        j++;
    }
return 0;
}

However, when the input is too larger, the program starts to print either multi-byte characters along with the correct string: (ؼ- 3) ) or if even bigger results in segmentation-fault.

Any idea what I am doing wrong when I am dynamically allocating memory?

reduce and fix your code:

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

char *add_to(char * copy, char ** array, int i , int j, int current_size){//i: start position, j: length of parts
    char *target = malloc(j + 1);//+1 for NUL
    strncpy(target, copy + i, j);
    target[j] = '\0';
    return array[current_size-1] = target;
}

int main(int argc, char** argv){
    const char *digits = "0123456789";
    char **array = NULL;
    int array_size = 0;//current size
    char *ts = argv[1];//terminal input

    int i, j;

    for(i = 0; ts[i] != '\0'; i += j){//i: index of the string
        //no need size check when expand one by one
        array = realloc(array, ++array_size * sizeof(char*));//fail check omitted
        j  = strspn(ts + i,      digits);//The length containing digit
        j += strcspn(ts + i + j, digits);//add length containing no digit
        add_to(ts, array, i, j, array_size);
    }
    /* print all string in array */
    for(j=0; j<array_size; j++){
        puts(array[j]);
    }
    //deallocate
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM