1

so i need to split the string given with:

const char *inputs[] = {"111adbhsd111gfhds","goal!","zhd!111oosd","111let111"};

to ouput:

char *outputs[]={"adbhsd","gfhds","goal!","zhd!","oosd","let"}

where the delimiter is : "111" . I tried with strtok , but as the delimiter is of mulitple character , it did't work!

any idea, how it might give the output, will help!

what i have did till now:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
size_t split(
    char **outputs, // outputs
    const char *separator, // the delimiter
    const char **inputs,
    size_t num_inputs // no. of input strings, given in input array
){
    size_t num_outputs = 0;
    int l= 0;
    
   for(size_t i = 0; i < num_inputs ; i++){
       if(strstr(*(inputs+i), separator) != NULL){  // to check, if the string of the given input array has the delimiter 
            char* pos = strstr( *(inputs+i), separator);
            
            //having problem in this part
            
        }
        else
        {
            strcpy( outputs[l] , *(inputs+i));;
            l++;
            num_outputs++;
        }
                
    }
        
    return num_outputs;
}
int main(){
        const char *inputs[] = {
        "111abdhsd111gfhds",
        "goal!",
        "zhd!111oosd",
        "111let111"
        };
        char *outputs[] ={malloc(1000),malloc(1000),malloc(1000),malloc(1000),malloc(1000),malloc(1000)};
        split(outputs, "111", inputs, 4);
        for(int i =0; i < 6; i++)
        {
                printf("The output[%d] is : %s" ,i, outputs[i]);
                free(outputs[i]); 
        
        }
        
        return 0;
        
    }
SJoNIne
  • 19
  • 2

1 Answers1

0

NOTE: The following answer refers to revision 2 of the question, which is before OP added code to the question which already uses the function strstr.


If the string is delimited by a substring instead of a single character, you can use the function strstr to find the delimiter substrings:

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

int main( void )
{
    const char input[] = "111adbhsd111gfhds", *p = input;
    const char *const delim = "111";

    //remember length of delimiter substring
    const size_t delim_length = strlen( delim );

    for (;;) //infinite loop, equivalent to "while ( 1 )"
    {
        //attempt to find next delimiter substring
        const char *q = strstr( p, delim );

        //break loop if this is last token
        if ( q == NULL )
            break;

        //print token
        if ( q == p )
            printf( "Found token: <empty>\n" );
        else
            printf( "Found token: %.*s\n", (int)(q-p), p );

        //make p point to start of next token
        p = q + delim_length;
    }

    //print last token
    printf( "Found token: %s\n", p );
}

This program has the following output:

Found token: <empty>
Found token: adbhsd
Found token: gfhds

Since the sample input starts with the delimiter "111", the first token is empty. If you don't want empty tokens to be printed, you can simply remove the first printf statement in the code.

This is not a full solution to your problem, as your task seems to consist of multiple input strings instead of only one, and writing to output arrays instead of printing to the screen. In accordance with the community guidelines for homework questions, I will not provide a full solution to your problem at this time. Instead, for now, I have only provided a solution to the problem that you stated that you had trouble with (which was using strtok with substring delimiters). If necessary, I can add additional code later.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • From the example in the question, it seems empty initial and final tokens should be ignored. – chqrlie Dec 08 '21 at 20:20
  • @chqrlie: I don't think that it is safe to assume that empty tokens should be ignored, even if OP's example implies that they should. Nevertheless, I have edited my answer to state that the corresponding `prinf` statement can simply be removed, if that is what OP wants. – Andreas Wenzel Dec 08 '21 at 20:43
  • @chqrlie true, we should ignore empty strings – SJoNIne Dec 08 '21 at 20:46