Search code examples
cstringfunctionstring-concatenation

Unable to concatenate two strings


I was practicing on the topics of string. I am trying to concatenate two strings without using the strcat() function available in C.

This is the code that I came up with:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int string_length();
void concatenate();

int main(void) {
    char str[] = "Hello there";
    char str2[] = "How are you";
    char result[100];
    int length;

    length = string_length(str);

    printf("%d\n", length);
    concatenate(result, str, str2);
}

//Function which counts the number of characters of the string
int string_length(char str[]) {
    int i = 0;
    while (str[i] != '\0') {
        i++;
    }
    return i;
}

//Function to concatenate the two strings
void concatenate(char result[], char str[], char str2[]) {
    int i = 0;
    while (str[i] != '\0') {
        result[i] = str[i];
        ++i;
    }
    int j = i + 2, k = 0;
    while (str2[k] != '\0') {
        result[j] = str2[k];
        k++;
        j++;
    }
    printf("%s\n", result);
}

When I ran this code, this is what I get as the output -

    11
    Hello there

The concatenation function is not working properly as output should be-

    11
    Hello there How are you

I used the debugger and found that loops are working fine and as per the counter variable, The two strings should get concatenated.


Solution

  • There are multiple problems in your code:

    • in your concatenation function, you initialize j to i + 2, whereas you should just use j = i as the first character of the second string should come just after the last character of the first and i was incremented after each character copied from str. Note that you can just use i, there is no need for j.
    • you must set the null terminator at the end of result. You can set at with result[j] = '\0';
    • you should output the concatenated string in main instead of concatenate().
    • the prototypes of string_length() and concatenate() should include the argument types.

    Also note that you should use size_t for the index types.

    Here is a modified version:

    #include <stdio.h>
    #include <string.h>
    
    size_t string_length(const char *s);
    void concatenate(char *result, const char *str, const char *str2);
    
    int main(void) {
        char str[] = "Hello there";
        char str2[] = "How are you";
        char result[100];
        size_t length;
    
        length = string_length(str);
    
        printf("%zu\n", length);
        concatenate(result, str, str2);
        printf("%s\n", result);  // shows Hello thereHow are you
        concatenate(result, "abc", "def");
        printf("%s\n", result);  // shows abcdef
        return 0;
    }
    
    //Function which counts the number of characters of the string
    size_t string_length(const char *s) {
        size_t i = 0;
        while (str[i] != '\0') {
            i++;
        }
        return i;
    }
    
    //Function to concatenate the two strings
    void concatenate(char *result, const char *str, const char *str2) {
        size_t i = 0;
        while (str[i] != '\0') {
            result[i] = str[i];
            ++i;
        }
        size_t k = 0;
        while (str2[k] != '\0') {
            result[i] = str2[k];
            k++;
            i++;
        }
        result[i] = '\0';
    }
    

    Here is an alternative approach, using pointer arithmetics:

    //Function to concatenate the two strings
    void concatenate(char *result, const char *str, const char *str2) {
        // copy the bytes from str, including the null terminator
        while ((*result = *str++) != '\0')
            result++;
    
        // copy the bytes from str2, including the null terminator
        while ((*result = *str2++) != '\0')
            result++;
    }