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

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. Commented Dec 20, 2019 at 20:20
  • Try malloc and read up about buffer overflows Commented Dec 20, 2019 at 20:21
  • Does this answer your question? How do I concatenate two strings in C? Commented Dec 20, 2019 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 ? Commented Dec 20, 2019 at 20:34
  • Post any update in the question (full implementation) so that we can revision it. Commented Dec 20, 2019 at 20:36

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.