How to concat integer and string inside recursive function in C -
guys, want make program generate combination elements. example if user input (n) = 3, output must :
1 12 123 13 2 23 3
i have problem concat integer , string inside recursive function..
code :
#include <stdio.h> void rekursif(int m, int n, char angka[100]){ int i; for(i=m; i<=n; i++){ sprintf(angka, "%s %d", angka, i); printf("%s \n", angka); rekursif(i+1,n,angka); } } int main(){ rekursif(1,5,""); return 0; }
when run program, command prompt not responding. guess, problem in concation ( sprintf(angka, "%s %d", angka, i); ) please me solve problem. thank :)
by passing ""
function argument, trying modify string literal, undefined behavior. instead, do:
int main(){ char str[100]; rekursif(1, 5, str); return 0; }
be careful buffer overflow in practice.
Comments
Post a Comment