Break text and store in vector

I want the program to be able to store only 1 Sentence per line. Being that each sentence is possible to be terminated according to the signs that I show in my code example. It does this but when it prints on the screen it does not print those same characters that indicate that it should change lines.

int main() {

//char str[] = "Ola. Tudo bem?\n Sim e contigo?\n Comigo esta tudo bem! Que tens feito?\n Trabalho no projeto!\n";
char str[] = "Utilizador 1 --> Bom dia! Tudo bem contigo?\nUtilizador 2 --> Comigo tudo excelente. Que tens feito?\nUtilizador 1 --> De momento estou a trabalhar num projeto e tu?\nUtilizador 2 --> Eu tenho estudado uma nova linguagem, Java. Bastante interessante. Devias experimentar.\nUtilizador 2 --> Talvez experimente quando tiver algum tempo livre!\n";
char **matriz = malloc(sizeof(char *) * 255);
int caractere,coluna,i;
int linha = 0;
matriz[linha] = malloc(255);
for (caractere = 0, coluna = 0; str[caractere] != '\0'; caractere++, coluna++) {
    if (str[caractere] == '?' || str[caractere] == '...' || str[caractere] == '!'  || str[caractere] == '.') {
        matriz[linha][coluna] = '\0';
        matriz[linha] = realloc(matriz[linha], coluna + 1);
        matriz[++linha] = malloc(255);
        coluna = -1;
    } else {
        matriz[linha][coluna] = str[caractere];
    }
}
matriz = realloc(matriz, sizeof(char *) * linha);
for (i = 0; i < linha; i++) {
    printf("%s\n", matriz[i]);
}

}

Author: Maniero, 2017-11-10

1 answers

Then it begins to complicate. I've made changes that solve the problem, but I don't even know if it's what you need, there's no clear definition of the problem. I interpreted that every line break should only be replaced. The remaining characters separate and then break.

I changed the strategy from the previous code. Now it takes all the characters and treats as exception only the line break character and another exception to treat the reticence since it is 3 characters. Can not buy directly more than one character, only if you use an auxiliary function.

I have not tested all situations. I actually think a lot can go wrong with this algorithm if the data is not well normalized. To make a robust algorithm would give a lot of work, it would already become a parser.

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

int main() {
    char str[] = "\nUtilizador 1 --> Bom dia! Tudo bem contigo?\n\nUtilizador 2 --> Comigo tudo excelente. Que tens feito?\nUtilizador 1 --> De momento estou a trabalhar num projeto... E tu?\nUtilizador 2 --> Eu tenho estudado uma nova linguagem, Java. Bastante interessante. Devias experimentar.\nUtilizador 2 --> Talvez experimente quando tiver algum tempo livre!\n";
    char **matriz = malloc(sizeof(char *) * 255);
    int linha = 0;
    matriz[linha] = malloc(255);
    for (int caractere = 0, coluna = 0; str[caractere] != '\0'; caractere++, coluna++) {
        matriz[linha][coluna] = str[caractere];
        if (str[caractere] == '\n' || str[caractere] == '\0' || str[caractere] == '.' || str[caractere] == '!' || str[caractere] == '?') {
            if (str[caractere] == '.' && str[caractere + 1] == '.' && str[caractere + 2] == '.') {
                matriz[linha][++coluna] = str[++caractere];
                matriz[linha][++coluna] = str[++caractere];
            }
            if (str[caractere] == '\n' && coluna == 0) {
                coluna = -1;
                continue;
            }
            if (str[caractere] == '\n' || str[caractere] == '\0') {
                matriz[linha][coluna] = '\0';
            } else {
                matriz[linha][++coluna] = '\0';
                caractere++;
            }
            matriz[linha] = realloc(matriz[linha], coluna + 1);
            matriz[++linha] = malloc(255);
            coluna = -1;
        }
    }
    matriz = realloc(matriz, sizeof(char *) * linha);
    for (int i = 0; i < linha; i++) {
        printf("%s\n", matriz[i]);
    }
}

See working on ideone. E no repl.it. also I put on GitHub for future reference .

 1
Author: Maniero, 2020-06-29 17:13:50