I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_FIL 15
#define MAX_COL 15
#define MAX 15*15
int frase_a_taula(char frase[], char taula[MAX_FIL][MAX_COL]) {
int i, j, k = 0, l, res = 0, final = 0;
for (i = 0; !final; i++) {
l = 0;
for (j = i; frase[j] != ' ' && frase[j] != '.'; j++) {
taula[k][l] = frase[j];
l++;
}
taula[k][l] = '\0';
if (frase[j] == '.') final = 1;
i = j;
k++;
res++;
}
return res;
}
void mescla_string_taula(char taula[MAX_FIL][MAX_COL], int n_filas, int coeficient) {
int n_mescles = 0, i, ind1, ind2;
if (coeficient == 2) {
n_mescles = n_filas / 2;
}
else if (coeficient == 1) {
n_mescles = n_filas / 4;
}
srand(time(NULL));
for (i = 0; i < n_mescles; i++) {
ind1 = rand() % n_filas;
ind2 = rand() % n_filas;
char *temp = (char *)malloc((strlen(taula[ind1]) + 1) * sizeof(char));
strcpy(temp, taula[ind1]);
strcpy(taula[ind1], taula[ind2]);
strcpy(taula[ind2], temp);
free(temp);
}
}
void alenteix_i_mescla_frase(char frase[MAX], char frase_lenta[MAX],
int velocitat, int coeficient) {
char taula[MAX_FIL][MAX_COL];
int i, j, len_frase_lenta;
int n_paraules = frase_a_taula(frase, taula);
mescla_string_taula(taula, n_paraules, coeficient);
strcpy(frase_lenta, taula[0]);
len_frase_lenta = strlen(frase_lenta);
for (i = 1; i < n_paraules; i++) {
if (velocitat == 1) {
if (i % 2 == 0) {
frase_lenta[len_frase_lenta] = '.';
frase_lenta[len_frase_lenta + 1] = '.';
frase_lenta[len_frase_lenta + 2] = '.';
len_frase_lenta += 3;
for (j = 0; j < strlen(taula[i]); j++) {
frase_lenta[len_frase_lenta] = taula[i][j];
len_frase_lenta++;
}
}
else {
frase_lenta[len_frase_lenta] = ' ';
len_frase_lenta++;
for (j = 0; j < strlen(taula[i]); j++) {
frase_lenta[len_frase_lenta] = taula[i][j];
len_frase_lenta++;
}
}
}
else if (velocitat == 2) {
frase_lenta[len_frase_lenta] = '.';
frase_lenta[len_frase_lenta + 1] = '.';
frase_lenta[len_frase_lenta + 2] = '.';
len_frase_lenta += 3;
for (j = 0; j < strlen(taula[i]); j++) {
frase_lenta[len_frase_lenta] = taula[i][j];
len_frase_lenta++;
}
}
}
frase_lenta[len_frase_lenta] = '.';
frase_lenta[len_frase_lenta+1] = '\0';
}
int main() {
printf("Introdueix nom del fitxer: ");
char path[MAX];
scanf("%s", path);
printf("Introdueix velocitat de parla: ");
int velocitat;
scanf("%d", &velocitat);
printf("Introdueix el coeficient de yodificacio: ");
int coef;
scanf("%d", &coef);
FILE* f = fopen(path, "r");
if (f == NULL) {
printf("Fitxer no trobat!");
}
else {
char line[MAX], frase_yodificada[MAX];
while (fgets(line, MAX, f) != NULL) {
alenteix_i_mescla_frase(line, frase_yodificada, velocitat, coef);
printf("%s\n", frase_yodificada);
}
printf("De processar...ha acabat, el fitxer\n");
}
return 0;
}
And the file yodatest.txt
may the force be with you.
may the force be with you every day of your life.
avui fa un dia molt bonic.
hola.
si entrenes molt i ets perseverant arribaras lluny i tindras molts poders.
un dia em comprare una moto i anire volant fins a l'estrella de la mort.
no hi ha dia que no m'agradi mirar les estrelles de nit.
When compiling and executing this code in Ubuntu wsl
under Windows
, I get no error whatsoever. However, when I compile the code and execute it in MacOs
, I get the output:
[1] 60023 trace trap ./main.exe
Sometimes it executes correctly, sometimes it doesn't, but I tried executing it under Ubuntu
several times and I always got the correct answer.
Also, commenting mescla_string_taula
seems to work, but again, I can't be sure due the nature of the error. Basically, that function just shuffles the matrix.
Any clue on why is this happening?
A correct example would be:
Introdueix nom del fitxer: yodatest.txt
Introdueix velocitat de parla: 1
Introdueix el coeficient de yodificacio: 1
may the...force you...with be.
with the...force be...may of...every day...you your...life.
avui fa...un bonic...molt dia.
hola.
si molts...molt i...ets poders...arribaras lluny...i perseverant...entrenes tindras.
volant anire...em comprare...una fins...i dia...un moto...a l'estrella...de la...mort.
no de...ha dia...que nit...m'agradi mirar...les no...hi estrelles.
De processar...ha acabat, el fitxer
i = j;
in routinefrase_a_taula
frase_a_taula
should take a string with spaces, always ending in '.', and put each word splitted in a different row. So in that line I'm copying the actual char in an empty row if it is not a space or '.'. If so, it needs to be in a new row.