Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[]){
char *line;
int number_words, number_space, size_line;
int space_per_word, upoloipo, i, j;
size_line = atoi (argv[1]);
line = (char*) malloc(sizeof(char)*size_line);
if ( line == NULL ) {
return 1;
}
for(i=0; i<size_line; i++){
line[i] = '\0';
}
do {
scanf("%d %d ", &number_words, &number_space);
if ( number_words != 0 ) {
space_per_word = number_space / (number_words - 1 );
upoloipo = number_space % (number_words - 1 );
for (i=1; i<=number_words; i++){
scanf("%s", line);
if (i < number_words ){
for (j=0; j<space_per_word +1; j++){
strcat(line, " ");
}
if (upoloipo >0){
strcat (line, " ");
upoloipo = upoloipo - 1;
}
}
else{
strcat (line, "\n");
}
printf ("%s", line);
}
}
}while ( number_words != 0 );
return (0);
}
this programm is called p3
i have a file that contains :
3 3 There is a 2 2 lovely road 2 4 that runs 2 3 from Ixopo 2 5 into the 2 1 hills. These 2 4 hills are 1 0 grass-covered 2 1 and rolling, 3 1 and they are 2 0 lovely beyond 2 2 any singing 3 3 of it. The 2 2 road climbs 2 2 seven miles 3 0 into them, to 1 1 Carisbrooke; 2 5 and from 2 4 there, if 3 2 there is no 2 4 mist, you 3 1 look down on 3 3 one of the 1 6 fairest 2 3 valleys of 1 6 Africa. 0
then :
gcc -Wall -g p3.c -o p3
and
./p3 13 < p2test
when i run it , it prints :
There is a
lovely road
that runs
from Ixopo
into the
hills. These
hills are
Floating point exception (core dumped)
what i did wrong ???