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.
#include <stdio.h>
#include <stdlib.h>
#define MAX 200
void concatination(char wc[], char concat[])
{
int x = 0, y = 0;
while (concat[x] != 0)
{
x++;
}
while (wc[y] != 0)
{
concat[x] = wc[y]; /* Append the entered string to the work string */
x++;
y++;
}
concat[x] = 0; /* Set the new NULL terminator for the work string */
return;
}
char * puissant(char w[], char p[], int t)
{
if (t>=1)
{
concatination(w, p);
puissant(w, p, t-1);
}
/* Works the same as the above block of code without recursion
for (int i = 0; i < t; i++)
{
concatination(w, p);
} */
return p;
}
int main()
{
char w1[MAX];
char p1[MAX];
int t, u;
while (1) /* Prompt for a valid combination of string and number of concatenations */
{
printf("Enter string: ");
if (fgets(w1, MAX - 1, stdin) == NULL)
{
continue;
}
for (u = 0; u < MAX; u++) /* Replace new line character with a NULL terminator in the string */
{
if(w1[u] < 12)
{
w1[u] = 0;
break;
}
}
printf("Enter number of concatenations: ");
u = scanf("%d",&t);
u = 0;
while (w1[u] != 0) /* Acquire string length without using "strlen" in the string.h include file */
{
u++;
}
u--;
if ((u * t + 1) < MAX)
{
break;
}
printf("Combination of string length and number of concatenations is too large - try again\n");
}
for (u = 0; u < MAX; u++) /* Initialize the work string */
{
p1[u] = 0;
}
printf("String: %s\n", puissant(w1, p1, t));
return 0;
}
Following are some of the highlights to this refactored code.
With the code refactored as such, following is some sample output at the terminal.
@Vera:~/C_Programs/Console/Combine/bin/Release$ ./Combine
Enter string: Hello Programmer
Enter number of concatenations: 5
String: Hello ProgrammerHello ProgrammerHello ProgrammerHello ProgrammerHello Programmer
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.