C homework - string loops replacements -
i know it's little unorthodox , cost me downvotes, since it's due in 1 hour , have no idea begin thought i'd ask guys.
basically i'm presented string contains placeholders in + form, example:
1+2+5 i have create function print out possibilities of placing different combinations of given series of digits. i.e. series:
[9,8,6] // string array the output be
16265 16285 16295 18265 18285 18295 19265 19285 19295 so each input (number of digits)^(number of placeholders) lines of output. digits 0-9 , maximum form of digits string [0,1,2,3,4,5,6,7,8,9]. original string can have many placeholders (as you'd expect output can lengthly).
i have in c, preferably no recursion. again appreciate help, couldn't more thankful right now.
if can offer idea, simplified way @ solving this, in different language or recursively, it'd still ok, use general concept , move on there.
it prints them in different order, not matter. , it's not recursive.
#include <stdlib.h> #include <stdio.h> int // 0 if no more. get_string(char* s, const char* spare_chr, int spare_cnt, int comb_num){ (; *s; s++){ if (*s != '+') continue; *s = spare_chr[comb_num % spare_cnt]; comb_num /= spare_cnt; }; return !comb_num; }; int main(){ const char* spare_str = "986"; int num = 0; while (1){ char str[] = "1+2+5"; if (!get_string(str, spare_str, strlen(spare_str), num++)) break; // done printf("str num %2d: %s\n", num, str); }; return 0; };
Comments
Post a Comment