program to concatenate two strings without using strcat() function.

29 Dec 2022 Balmiki Kumar 0 C Programming

Concatenate Strings Without strcat() - Efficient String Manipulation

strcat(string1,string2) is a C standard function declared in the header file string.h, The strcat() function concatenates string2, string1 and returns string1.

C program to String Concatenation in C Without strcat() - Step-by-Step Guide

#include<stdio.h>
#include<string.h>
char *strct(char *c1, char *c2);
char *strct(char *c1, char *c2) {
 //strlen function returns length of argument string
 int i = strlen(c1);
 int k = 0;
 //loops until null is encountered and appends string c2 to c1
 while (c2[k] != '\0') {
 c1[i + k] = c2[k];
 k++;
 }
 return c1;
}
int main() {
 char string1[15] = "first";
 char string2[15] = "second";
 char *finalstr;
 printf("Before concatenation:"
 " \n string1 = %s \n string2 = %s", string1, string2);
 //addresses of string1, string2 are passed to strct()
 finalstr = strct(string1, string2);
 printf("\nAfter concatenation:");
 //prints the contents of string whose address is in finalstr
 printf("\n finalstr = %s", finalstr);
 //prints the contents of string1
 printf("\n string1 = %s", string1);
 //prints the contents of string2
 printf("\n string2 = %s", string2);
 return 0;
}

Output:

Before concatenation:
string1 = first
string2 = second
After concatenation:
finalstr = firstsecond
string1 = firstsecond
string2 = second

Explanation:

string2 is appended at the end of string1 and contents of string2 are unchanged. In strct() function, using a for loop, all the characters of string 'c2' are copied at the end of c1. return (c1) is equivalent to return &c1[0] and it returns the base address of 'c1'. 'finalstr' stores that address returned by the function strct().

 

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

// Function to concatenate two strings
void concatenateStrings(char destination[], const char source[]) {
    int destLen = strlen(destination);
    int i, j;

    for (i = destLen, j = 0; source[j] != '\0'; i++, j++) {
        destination[i] = source[j];
    }

    destination[i] = '\0'; // Null-terminate the concatenated string
}

int main() {
    char title[100] = "Page Title - ";
    char subtitle[] = "Sub Title";
    
    concatenateStrings(title, subtitle);
    
    printf("Title: %s\n", title);

    // Creating meta tags
    char metaTitle[150] = "Meta Title: ";
    char metaDescription[200] = "Meta Description: ";
    char keyword1[] = "keyword1";
    char keyword2[] = "keyword2";

    concatenateStrings(metaTitle, title);
    concatenateStrings(metaDescription, subtitle);
    concatenateStrings(metaDescription, " with keywords: ");
    concatenateStrings(metaDescription, keyword1);
    concatenateStrings(metaDescription, ", ");
    concatenateStrings(metaDescription, keyword2);

    printf("Meta Title: %s\n", metaTitle);
    printf("Meta Description: %s\n", metaDescription);

    return 0;
}

In this program, the concatenateStrings function takes a destination string and a source string as arguments and appends the characters from the source string to the end of the destination string. The null-terminator is then added at the end to ensure the concatenated string is properly terminated.

Keep in mind that this is a basic example and doesn't handle edge cases like buffer overflow. In a real-world scenario, you would need to ensure that your destination string has enough space to accommodate the concatenated content.

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

Design a function to find string length in c

Design a Function to print the string character by character

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 Kumar

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.