0

Exercise:

Write a function that returns a CH character string after adding a CH1 string at the end of a CH2 string given as a parameter.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

char *ajout(char ch1[], char ch2[]);

int main() {
    char ch1[] = "hello";
    char ch2[] = "ooo";
    char *ch = ajout(ch1, ch2);
    printf("%s",ch);
    return 0;
}

char *ajout(char ch1[], char ch2[]) {
    char *ch;
    int nb = 0;
    for (int i = 0; ch1[i] != '\0'; i++) {
        ch[i] = ch1[i];
        nb++;
    }
    int i = 0;
    for (i; ch2[i] != '\0'; i++) {
        ch[nb] = ch2[i];
        nb++;
    }
    return ch;
}

The expected result after program execution: helloooo

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yassel
  • 77
  • 1
  • 1
  • 7
  • 2
    `char *ch;` has no memory allocated (in the function `ajout`). Try `ch = malloc(1 + strlen(ch1) + strlen(ch2));` And you should add a nul terminator to the concatenated string. – Weather Vane Dec 20 '19 at 20:20
  • Try malloc and read up about buffer overflows – Ed Heal Dec 20 '19 at 20:21
  • Does this answer your question? [How do I concatenate two strings in C?](https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c) – Alexander van Oostenrijk Dec 20 '19 at 20:23
  • thank you @weathervane that's what i do :` int c = malloc(1 + strlen(ch1) + strlen(ch2)); char ch[c]; *ch=ajout(ch1,ch2);` but it's still not woking,what's the wrong with my code ? – Yassel Dec 20 '19 at 20:34
  • Post any update in the question (full implementation) so that we can revision it. – Roberto Caboni Dec 20 '19 at 20:36
  • That's become a muddle, sorry. You allocate memory for the new array and assign its pointer to an `int`. Then you define a variable length array, using an address as its length. The two changes I suggested are really rather simple. I spelled out the first and left the second (the nul terminator) for you. – Weather Vane Dec 20 '19 at 20:37
  • thank you @alexandervanoostenrijk ,not really i saw the ansewrs but they use strcpy function , in this exercice i should not use another function – Yassel Dec 20 '19 at 20:40

1 Answers1

3

First, a string in C is just a pointer to an array of char that is terminated by the first null character. There is no string concatenation operator in C.

Also, in order to create a new array in C - you need to allocate memory for it using malloc. However, in the ajout function, you don't allocate any memory for ch. Solution:

const size_t len1 = strlen(ch1); //get ch1 length
const size_t len2 = strlen(ch2); //get ch2 length
char *ch = malloc(len1 + len2 + 1); //create ch with size len1+len2+1 (+1 
                                    //for null-terminator)

I see you copy each char of ch1 and ch2 into ch one by one. A better solution would be to copy ch1 and ch2 using memcpy. Also, after allocation memory using malloc, you need to deallocate the string using free:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* ajout(const char *ch1, const char *ch2)
{
    //get length of ch1, ch2
    const size_t len1 = strlen(ch1);
    const size_t len2 = strlen(ch2);

    //create new char* array to save the result 
    char *result = malloc(len1 + len2 + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here

    //copy ch1, ch2 to result
    memcpy(result, ch1, len1); 
    memcpy(result + len1, ch2, len2 + 1); // +1 to copy the null-terminator
    return result;
}

int main()
{
    char ch1[] = "hello";
    char ch2[] = "ooo";
    char *ch = ajout(ch1,ch2);
    printf("%s",ch);
    free(ch); // deallocate the string
    return 0;
}

output: helloooo

You can read more about memcpy here and about malloc here.