Skip to content
Snippets Groups Projects

VSS: P4 A3

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Daniil Ossinkin
    Edited
    main.c 1.18 KiB
    #include <stdio.h>
    #include "stdlib.h"
    #include "string.h"
    #include <time.h>
    
    char* substring(char* str, int from, int to) {
        char* sub =  malloc(sizeof(char)*(to - from));
        memcpy(sub, str + from, to - from + 1);
        return sub;
    }
    
    char** get_all_possible_substring(char* string, int* count) {
        size_t len = strlen(string);
    
        int count_of_substrings = (int)((float)(len*(len + 1)) * 0.5f);
        char** substrings = malloc(sizeof(char*) * count_of_substrings);
        int counter = count_of_substrings;
    
        for(int i = 0; i < len; i++) {
            for(int j = 0; j < i + 1;j++, counter--) {
                substrings[counter] = substring(string, j, j + len - i );
            }
        }
    
        *count = count_of_substrings - 1;
        return substrings;
    }
    
    int main(int argc, char** argv) {
        int bufferSize = 1 << 16;
        char buffer[bufferSize];
    
        char* readed = fgets(buffer, bufferSize, stdin);
    
        int count_of_substring;
        char** substrings = get_all_possible_substring(readed, &count_of_substring);
    
        while(count_of_substring) {
    
            char* substring = substrings[count_of_substring--];
            printf("%s", substring);
    
            free(substring);
        }
    
        free(substrings);
        return 0;
    }
    
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment