Search code examples
crecursionstring-concatenationlanguage-theory

concatenation two strings recursive


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


Solution

  • 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:

    • The code does not rely on utilizing string manipulation functions such as "strlen" (string length) and "strcat" (string concatenation) as found in the standard include file, "string.h".
    • Recursive calls would be used in lieu of possibly looping.

    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.

    • In reviewing the usage of the concatenation function, there was no requirement of a character array being returned, so this function was redefined to have a void return value.
    • The code in the concatenation function was simplified to just keep appending the entered string value to the result string each time the function is called.
    • Although function "puissant" is being called recursively to achieve the desired repeated concatenation, a commented-out block of code is sitting in the source code to illustrate how this could have also been accomplished with a simple "for" loop.
    • Per the helpful comments, the "fgets" function is used in lieu of the "gets" function, and string sizes are based upon a defined maximum value to allow for testing of larger strings and/or larger concatenation values.
    • Some bounds checking was added to ensure that the string length and concatenation value do not exceed the maximum size of the strings to make the program more robust.

    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.