strcat without using built in function

Certainly! Here is an example of how to implement the strcat function in C++ without using the built-in function:

#include <iostream>

void strcat_custom(char destination, const char source) {
    // Find the end of the destination string
    while (*destination) {
        destination++;
    }

    // Copy the characters from the source string to the end of the destination string
    while (*source) {
        destination = source;
        destination++;
        source++;
    }

    // Add the null terminator to the end of the concatenated string
    *destination = '\0';
}

int main() {
    char str1[20] = "Hello";
    const char* str2 = " World!";

    strcat_custom(str1, str2);
    std::cout << str1 << std::endl;

    return 0;
}

Explanation:

  1. The strcat_custom function takes two parameters: destination (the string to append to) and source (the string to append).
  2. We start by finding the end of the destination string. This is done by iterating through the characters of the string until we reach the null terminator ('\0').
  3. Once we have reached the end of the destination string, we start copying the characters from the source string to the end of the destination string. This is done by iterating through the characters of the source string and copying each character to the destination string.
  4. After copying all the characters from the source string to the destination string, we add the null terminator ('\0') to the end of the concatenated string to ensure it is properly terminated.
  5. In the main function, we demonstrate the usage of the strcat_custom function by concatenating the strings "Hello" and " World!". The result is stored in the str1 array, and then printed to the console using std::cout.

This implementation of strcat without using the built-in function showcases how you can manually concatenate two strings by iterating through the characters and copying them to the destination string.