
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- JavaScript - Remove first n characters from string
- How OSPF routes the packets from source to destination?
- 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
- Removing n characters from a string in alphabetical order in JavaScript
- 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?
- How to get last 4 characters from string in C#?
- How to remove certain characters from a string in C++?
- Possible walks from a source to a destination with exactly k edges
- Count all possible walks from a source to a destination with exactly k edges in C++
- Program to find lexicographically smallest string to move from start to destination in Python
- 8085 program to move blocks of bits from source location to a destination location
Advertisements