So I have an array of char called s.
This array has many words inside, each one separated by '\0'.
The words are sorted by length (from bigger to smaller).
I have have a char matrix with random things inside (it was not initialized) caled mat.
I want to copy the first word from the array s to the matrix mat.
Code:
int nlin, ncol; /*number of lines and collumns.*/
int c,l,a,q;
char mat [1000][1000];
char s[1000];
/*insert first word from s on mat*/
for(l=0,c=0,a=0,q=0;(strlen(s)<=(ncol*nlin))&&(s[a]!='\0');q++){
/*the word from s is printed on mat until s[a] reaches the end of the word. If the word has more chars in number then spaces on the matrix, this for is just ignored.*/
for(;((c<ncol-q-1)&&(s[a]!='\0'));a++,c++){
/*inside this for, we print the word from the array s through the matrix's first line's columns from first to last.*/
mat[l][c]=s[a];
}
for(;((l<nlin-q-1)&&(s[a]!='\0'));a++,l++){
/*inside this for, we print the word from the array s through the matrix's last coumns' lines from first to last.*/
mat[l][c]=s[a];
}
for(;((c>q)&&(s[a]!='\0'));a++,c--){
/*inside this for, we print the word from the array s through the matrix's last line's columns from last to first.*/
mat[l][c]=s[a];
}
for(;((l>q)&&(s[a]!='\0'));a++,l--){
/*inside this for, we print the word from the array s through the matrix's first column's lines from last to first.*/
mat[l][c]=s[a];
}
}
if (s[a]!='\0') {printf("Impossible to constroy matrix\n");return 0;}
/*print matrix*/
for (l=0;l<nlin;l++) {
for(c=0;c<ncol;c++) {
printf("%c",mat[l][c]);
}
printf("\n");
}
return 0;
}
I can't see where this is wrong, but, when i test it, it clearly is not right. for example, if the input is 3 lines and 3 columns for the matrix and the word is crate, the output is :
cra
t
e
but it should be:
cra
t
e
Where did i go wrong?