Hello everybody,
I'm a bit of a newbie with c. I've written the following program to generate a look-and-say sequence (Look-and-say sequence - Wikipedia, the free encyclopedia). It should ask for a number and then generate that number of items in the sequence. However, it doesn't generate more than 2 numbers, after that it just segfaults.
Does anybody have a clue where I'm going wrong with this one?
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLENGTH 1000 void gen_next_conway(char *cur_conway, char *new_conway, int max); void getline(char *line, int max); main() { char conway[MAXLENGTH] = "312211"; char newconway[MAXLENGTH]; char *curconway = conway; char c_count[10]; int count, i; printf("Hoeveel conway nummers wil je zien? "); getline(c_count, 10); count = atoi(c_count); for (i = 0; i < count; i++) { printf("Current conway at %d: %s\n", i, curconway); printf("Newconway at %d: %s\n", i, newconway); gen_next_conway(curconway, newconway, MAXLENGTH); printf("%s\n", newconway); curconway = newconway; } } void gen_next_conway(char *cur_conway, char *new_conway, int max) { int digit_count; char cur_digit; char *digitcounter; while (*cur_conway) { cur_digit = *cur_conway; for(digit_count = 0; *cur_conway == cur_digit; digit_count++, cur_conway++) ; sprintf(new_conway, "%d", digit_count); new_conway++; *new_conway = cur_digit; new_conway++; } *new_conway = '\0'; } void getline(char *line, int max) { char c; int i = 0; while ((c = getchar()) != EOF && c != '\n' && i < max) { *line = c; line++; i++; } }



LinkBack URL
About LinkBacks


