Add source string at the end of destination string in c

27 Sep 2022 Balmiki Mandal 0 C Programming

Concatenating Strings in C: Adding Source to Destination

To add a source string at the end of a destination string in C, you can use the following steps:

  1. Declare a pointer to the destination string.
  2. Declare a pointer to the source string.
  3. Find the end of the destination string by iterating through it until you reach the null character (\0).
  4. Copy the source string to the end of the destination string, starting at the position you found in step 3.
  5. Add a null character to the end of the destination string.

Example of a C function that adds a source string at the end of a destination string:

C Programming
#include <string.h>

char *add_source_string_at_the_end_of_destination_string(char *destination_string, char *source_string) {
  // Find the end of the destination string.
  char *destination_string_end = destination_string;
  while (*destination_string_end != '\0') {
    destination_string_end++;
  }

  // Copy the source string to the end of the destination string.
  while (*source_string != '\0') {
    *destination_string_end = *source_string;
    destination_string_end++;
    source_string++;
  }

  // Add a null character to the end of the destination string.
  *destination_string_end = '\0';

  // Return the destination string.
  return destination_string;
}

To use the add_source_string_at_the_end_of_destination_string() function, you would simply pass in the destination string and the source string as arguments. The function would then return a pointer to the destination string with the source string appended to the end.

Example of how to use the add_source_string_at_the_end_of_destination_string() function:

C Programming
#include <stdio.h>
#include <string.h>

int main() {
  char destination_string[] = "This is a test string.";
  char source_string[] = " This is a source string.";

  // Add the source string to the end of the destination string.
  char *new_string = add_source_string_at_the_end_of_destination_string(destination_string, source_string);

  // Print the new string.
  printf("%s\n", new_string);

  return 0;
}

Output:

 

This is a test string. This is a source string.

Further Reading:


Implementation of strncat() in C

add source string at the end of destination string

comparing two string using strcmp and strncmp

Design a function to copy to copy string into destination string using strcpy and strncpy

how to implement implementation of strncpy in c

Design a function to calculate the length of the string using strlen

pre defined string based function

Design a function using my_strcpy and using my_strchr we need to select given char in given string

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.