Seven7UP

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.1 kB
9
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

#define MAX_STR_LEN 70
#define MAX_SEP_LEN 3

int len(char* word){
    int length;
    for (length = 0; word[length] != '\0'; length++);
    return length;
}

struct String {
  char *original_string, *separator;
  char **string_separated;
  int string_len, sep_len, substrings_counter;
};


void copy(char *src, char *dest, int start, int length){
    int i;
    for (i = 0; i < length; i++){
        dest[i] = src[start + i];
    }
}


void add(struct String * string, int start, int length){
	string->string_separated = (char **) realloc (string->string_separated ,sizeof(char*) * (string->substrings_counter + 1));
	string->string_separated[string->substrings_counter] = (char *) malloc(sizeof(char) * length + 1);
	copy(string->original_string, string->string_separated[string->substrings_counter], start - length, length);
	string->substrings_counter++;
}

void split(struct String * string){
    int curr_length = 0, terminated = 0, i = 0;
    for (; i < string->string_len; i++){
        int j = 0;
        for (; j < string->sep_len; j++){
            if (string->original_string[i + j] != string->separator[j]){
                break;
            }
        }
        if (j == string->sep_len){
        	add(string, i, curr_length);
            curr_length = 0;
            terminated = 1;
            i += string->sep_len - 1;
        }else{
            curr_length++;
            terminated = 0;
        }
    }
    if (!terminated){
    	add(string, i, curr_length);
    }

}
int main()
{
    int series_length = 0, i, j;
    printf("insert series length: ");
    scanf("%d", &series_length);

    struct String *strings = (struct String *) malloc (sizeof(struct String) * series_length);

    for (i = 0; i < series_length; i++){
        strings[i].original_string = (char *) malloc(sizeof(char) * MAX_STR_LEN + 1);
        printf("insert string #%d: ", i + 1);
        scanf("%s", strings[i].original_string);
        strings[i].string_len = len(strings[i].original_string);

        strings[i].separator = (char *) malloc(sizeof(char) * MAX_SEP_LEN + 1);
        printf("insert separator #%d:", i + 1);
        scanf("%s", strings[i].separator);
        strings[i].sep_len = len(strings[i].separator);
    }

    for (i = 0; i < series_length; i++){
        split(&strings[i]);
    }

    for (i = 0; i < series_length; i++){
        printf("Original String: \"%s\" Separator string: \"%s\"\n", strings[i].original_string, strings[i].separator);
        for (j = 0; j < strings[i].substrings_counter; j++){
            printf("%s\n", strings[i].string_separated[j]);
        }
        printf("\n");
    }

    for (i = 0; i < series_length; i++){
    	free(strings[i].original_string);
    	free(strings[i].separator);
    	for (j = 0; j < strings[i].substrings_counter; j++){
    		free(strings[i].string_separated[j]);
    	}
    	free(strings[i].string_separated);
    }
    free(strings);

    return 0;
}