
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Concatenating n characters from source string to destination string in C
Problem
Write a C program to concatenate n characters from source string to destination string using strncat library function
Solution
The strcat function
This function is used for combining or concatenating two strings.
The length of the destination string must be greater than the source string.
The resultant concatenated string will be in the source string.
Syntax
strcat (Destination String, Source string);
Example 1
#include <string.h> main(){ char a[50] = "Hello"; char b[20] = "Good Morning"; clrscr ( ); strcat (a,b); printf("concatenated string = %s", a); getch ( ); }
Output
Concatenated string = Hello Good Morning
The strncat function
This function is used for combining or concatenating n characters of one string into another.
The length of the destination string must be greater than the source string.
The resultant concatenated string will be in the destination string.
Syntax
strncat (Destination String, Source string,n);
Example 2
#include <string.h> main ( ){ char a [30] = "Hello"; char b [20] = "Good Morning"; clrscr ( ); strncat (a,b,4); a [9] = ‘\0’; printf("concatenated string = %s", a); getch ( ); }
Output
Concatenated string = Hello Good.
Example 3
#include<stdio.h> #include<string.h> void main(){ //Declaring source and destination strings// char source[45],destination[50]; //Reading source string and destination string from user// printf("Enter the source string :"); gets(source); printf("Enter the destination string before :"); gets(destination); //Concatenate all the above results// destination[2]='\0'; strncat(destination,source,2); strncat(destination,&source[4],1); //Printing destination string// printf("The modified destination string :"); puts(destination); }
Output
Enter the source string :TutorialPoint Enter the destination string before :C Programming The modified destination string :C Tur
- Related Articles
- JavaScript - Remove first n characters from string
- Possible walks from a source to a destination with exactly k edges\n
- Print all paths from a given source to a destination in C++
- C Program to delete n characters in a given string
- C# Program to remove duplicate characters from String
- How OSPF routes the packets from source to destination?
- Removing n characters from a string in alphabetical order in JavaScript
- How to remove certain characters from a string in C++?
- Print all paths from a given source to a destination using BFS in C++
- How to extract the first n characters from a string using Java?
- How to extract the last n characters from a string using Java?
- Program to find lexicographically smallest string to move from start to destination in Python
- How to get last 2 characters from string in C# using Regex?
- Remove characters from a string contained in another string with JavaScript?
- Count all possible walks from a source to a destination with exactly k edges in C++

Advertisements