-1

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

imad
  • 3
  • 2
  • 2
    Stop using `gets()` immediately. It's a dangerous function because you can't specify the buffer size, and it has been removed from the language. Use `fgets()` instead and [remove the newline yourself](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input) – Barmar Feb 24 '23 at 23:06
  • Please use better variable names, like `source` and `dest`. – Barmar Feb 24 '23 at 23:08
  • 1
    This is a good opportunity to learn to use a debugger. Step through the code and see when things are not as you expect. – Barmar Feb 24 '23 at 23:10
  • i'm new in the field iam sorry if i bother you – imad Feb 24 '23 at 23:17
  • 3
    Note that both `w1` and `p` are 20 chars wide, so that they can store strings of 19 characters (plus the terminator). Is that enough? BTW, why are you trying to do it recursively, can't you use loops? – Bob__ Feb 24 '23 at 23:29
  • nah didn't work – imad Feb 24 '23 at 23:31
  • the problem is in the second function – imad Feb 24 '23 at 23:31
  • is there a better syntax to write it – imad Feb 24 '23 at 23:31
  • You might want to look at your`puissant` function and how it uses `concatination` function, or what arguments it uses and what will be in `p` after each recursion step. Note also that @Bob__ is correct that 20 characters is rather short for this kind of thing unless you are only concatenating short strings a few times, it would be easier to work with larger buffers to avoid overflows (or better, add checks to make sure your functions will not overflow the buffers them write to by providing a size argument as well as the buffer pointer) – Unn Feb 25 '23 at 00:29

1 Answers1

0

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.

NoDakker
  • 3,390
  • 1
  • 10
  • 11