i tried to concatinate a string N times using recursuve functions concatination and puissant in c
and i expacted that when the user enter the string and how much times he wants to repeat it
the string will be concatenated N times
i tried this code and it did not do the job
char* concatination(char w1[] , char w2[] , char concat[]){
if (*w1 != '\0'){
*concat = *w1;
concatination(w1+1,w2,concat+1);}
if (*w2 != '\0' && *w1 == '\0'){
*concat = *w2;
concatination(w1,w2+1,concat+1);
}
if (*w2 == '\0' && *w1 == '\0'){
*concat = '\0';
}
return concat;
}
char* puissant(char w[],char p[],int t){
if (t>=1){
concatination(w,w,p);
puissant(w , p , t-1);
}
return p;
}
int main(){
char w1[20];
char p[20];
int t;
gets(w1);
scanf("%d",&t);
puts(puissant(w1,p,t));
return 0;
}
the problem is it's only apears 2 times any help please
I tried out your code and did run into problems getting it to perform as required. So with that, I did a bit of refactoring keeping the following apparent requirements in play in the refactored code:
With that in mind and also being mindful of the valuable comments made above, I built the following refactored code.
Following are some of the highlights to this refactored code.
With the code refactored as such, following is some sample output at the terminal.
Give those refactored bits a try and see if it meets the spirit of your project. Also, you might look into the available string manipulation functions in the "string.h" include file to simplify things even further.