Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Print the Length of a String using Pointers
Next article icon

C Program to Concatenate Two Strings Without Using strcat

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

String concatenation is the process of appending one string to the end of another. C language provides strcat() library function to concatenate string but in this article, we will learn how to concatenate two strings without using strcat() in C.

The most straightforward method to concatenate two strings without using strcat() is by using a loop. Let’s take a look at an example:

C
#include <stdio.h>  void concat(char *s1, char *s2) {     int i = 0;        // Move to the end of str1     while (s1[i] != '\0')         i++;      // Copy characters from str2 to str1     int j = 0;     while (s2[j] != '\0') {         s1[i] = s2[j];           i++;         j++;     }      // Null-terminate the concatenated string     s1[i] = '\0'; }  int main() {     char s1[50] = "Hello ";     char s2[] = "Geeks";      concat(s1, s2);      printf("%s", s1);     return 0; } 

Output
Hello Geeks 

Explanation: In this method, we iterate through the first string to find its end, then use a loop to append each character of the second string to the end of first string.

There are also a few other methods in C to concatenate two strings without using strcat. Some of them are as follows:

Table of Content

  • Using Pointers
  • Using Memcpy
  • Using sprintf()

Using Pointers

In this method, we use pointer arithmetic to traverse and concatenate the two strings. First, move the pointer to the end of the first string, then copy each character from second string one by one.

C
#include <stdio.h>  void concat(char *s1, char *s2) {        // Move the pointer to the end of str1     while (*s1)         s1++;        // Copy characters from s1 to s1 using   	// pointer arithmetic     while (*s2) {         *s1 = *s2;         s1++;                   s2++;              }      *s1 = '\0'; }  int main() {     char s1[50] = "Hello ";     char s2[] = "Geeks";    	// Concat string s1 and s2     concat(s1, s2);      printf("%s", s1);       return 0; } 

Output
Hello Geeks

This method is efficient because it directly works with memory addresses and does not require array indexing, making it a bit more low-level.

Using memcpy()

The memcpy() is a function in C that copies a block of memory from a source location to a destination. It can be used to copy the second-string memory at the end of first string.

C
#include <stdio.h> #include <string.h>  void concat(char *s1, const char *s2) {      // Copy s2 to the end of s1 using memcpy()     memcpy(s1 + strlen(s1), s2, strlen(s2) + 1); }  int main() {     char s1[50] = "Hello";     char s2[] = " Geeks";      // Concatenate using memcpy()     concat(s1, s2);      printf("%s", s1);     return 0; } 

Output
Hello Geeks

Using sprintf()

The sprintf() function in C can write a formatted string to a string buffer. It can be used to concatenate string by formatting second string at the end of the first string by treating it as a string buffer.

C
#include <stdio.h> #include <string.h>  int main() {     char s1[50] = "Hello ";      char s2[] = "Geeks";      // Concatenate s2 to s1 using sprintf     sprintf(s1 + strlen(s1), "%s", s2);      printf("%s", s1);     return 0; } 

Output
Hello Geeks

Next Article
C Program to Print the Length of a String using Pointers
author
fizakhatoon947
Improve
Article Tags :
  • C Programs
  • C Language
  • C-String
  • C Examples

Similar Reads

  • C Program to Compare Two Strings Without Using strcmp()
    String comparison refers to the process of comparing two strings to check if they are equal or determine their lexicographical order. C provides the strcmp() library function to compare two strings but in this article, we will learn how to compare two strings without using strcmp() function. The mos
    2 min read
  • C Program to Concatenate Two Strings Using a Pointer
    Concatenating two strings means appending one string at the end of another string. While the standard library provides strcat() for concatenation, this article will demonstrate how to concatenate two strings using pointers. To concatenate two strings using pointers, traverse the first string to its
    1 min read
  • C Program to Compare Two Strings Using Pointers
    In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers. To compare two strings using pointers, increment the pointers to traverse through each character of the strings whi
    2 min read
  • Concatenating Two Strings in C
    Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C. The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example: [GFGTABS] C #include <s
    3 min read
  • C Program to Print the Length of a String using Pointers
    C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C. The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character o
    2 min read
  • Length of a String Without Using strlen() Function in C
    The length of a string is the number of characters in it without including the null character. C language provides the strlen() function to find the lenght of the string but in this article, we will learn how to find length of a string without using the strlen() function. The most straightforward me
    3 min read
  • C Program to Sort a String of Characters
    Sorting a string of characters refers to the process of rearranging all the characters in the given order. In this article, we will learn how to sort a string of characters using the C program. The most straightforward method to sort a string of characters is by using the qsort() function. Let's tak
    3 min read
  • C Program to Print the Length of a String Using %n Format Specifier
    In C, strings are arrays of characters terminated by a null character ('\0') and the length of a string is the number of characters before this null character. In this article, we will learn how to find the length of the string using %n format specifier. The %n is a special format specifier for prin
    1 min read
  • C/C++ program to print Hello World without using main() and semicolon
    The task is to write a program to print Hello World without using main() and semicolon. As we already know, how to print Hello World without the use of a semicolon. Now, for writing without main() method, we will need a Macro. C/C++ Code // C program to print Hello World // without using main() and
    1 min read
  • C Program to Trim Leading Whitespaces from String
    Trimming leading white spaces from a string means removing any unnecessary spaces before the first non-space character in the string. In this article, we will learn how to trim leading white spaces using the C program. The most straightforward method to trim leading white spaces from the string is b
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences