- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Program to Concatenate Two Strings
Here you will learn about and get code for string concatenation in C++. The program to concatenate strings is created in the following ways:
- String concatenation without using the library function strcat()
- Concatenate two strings using strcat()
- String concatenation using the + operator
- String concatenation using pointers
String concatenation without strcat() in C++
This program concatenates or appends a second string to the first without using any library functions like strcat().
#include<iostream> #include<stdio.h> using namespace std; int main() { char strOne[100], strTwo[50], i, j=0; cout<<"Enter the First String: "; gets(strOne); cout<<"Enter the Second String: "; gets(strTwo); for(i=0; strOne[i]!='\0'; i++) j++; for(i=0; strTwo[i]!='\0'; i++) { strOne[j] = strTwo[i]; j++; } strOne[j] = '\0'; cout<<"\nString after Concatenation: "<<strOne; cout<<endl; return 0; }
This program was built and runs under the Code::Blocks IDE. Here is its sample run:
Now supply any string as input, say codes, and then another string as input, say cracker. Press the
ENTER
key to add the second string "cracker" to the first "codes," as shown in the output given
below:
Here is another sample run with user input; this is is the first string and fresherearth is the second string:
Note: Don't forget to enter a space after this is or before fresherearth while entering the first or second string. Otherwise both the strings gets concatenated without space like this isfresherearth.
The following two statements:
strOne[j] = strTwo[i]; j++;
can also be replaced with:
strOne[j++] = strTwo[i];
That is, a post increment to j is used, which increments its value by 1 after evaluating the statement:
strOne[j] = strTwo[i];
If the user enters the first string as codes, then it gets initialized to strOne in a way that:
- The first character (c) is set to strOne[0]
- Similarly, strOne[1]=o, strOne[2]=d, strOne[3]=e, strOne[4]=s
And if the user enters cracker as a second string, then it also gets initialized to strTwo in a similar way. Therefore, with this two-string input, the dry run of the above program goes like this:
- At the first execution of the for loop, 0 gets initialized to i and checks the condition, strOne[i]!='\0', strOne[0]!='\0', or c!='\0'.
- The condition is evaluated as true. Because c is not equal to a null-terminated character
- Therefore, program flow goes inside the loop and increments the value of j.
- Then the program flow goes back to the update part of the for loop, increments the value of i, and checks for the condition again with the new value of i.
- This process continues until the condition is evaluated as true.
- Because the first string is codes, at the fifth time, the condition evaluates to be false, and the program flow exits the loop and goes to second for loop. Now the value of j is 5.
- Therefore, from the 5th index, all the characters (one by one) of the second string gets initialized
to the first string in a way that:
- The first character (c) of the second string gets initialized to strOne[5].
- The second character (r) of the second string gets initialized to strOne[6].
- Similarly, strOne[7] = a, strOne[8] = c, strOne[9] = k, strOne[10] = e, and strOne[11] = r.
- After the loop is completed, \0 is initialized to strOne[12].
- Print the value of strOne that equals fresherearth.
String concatenation using strcat() in C++
The function strcat() takes two strings as arguments. The value of the second string gets appended or concatenated to the first string. For example, if there are two strings, say strOne and strTwo. And the value of both strings are:
strOne = "codes"; strTwo = "cracker";
Then,
strcat(strOne, strTwo);
That means strOne is now equal to fresherearth. That is, the value of the second string concatenated to the first one. This function is defined in a string.h library.
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int main() { char strOne[100], strTwo[50]; cout<<"Enter the First String: "; gets(strOne); cout<<"Enter the Second String: "; gets(strTwo); strcat(strOne, strTwo); cout<<"\nString after Concatenation: "<<strOne; cout<<endl; return 0; }
This program will produce the same output as the previous one.
String Concatenation using the + Operator in C++
The question is: write a program in C++ to concatenate two strings using the + operator. The answer to this question is given below:
#include<iostream> using namespace std; int main() { string strOne="", strTwo="", strThree=""; cout<<"Enter the First String: "; cin>>strOne; cout<<"Enter the Second String: "; cin>>strTwo; strThree = strOne + strTwo; cout<<"\nString after Concatenation: "<<strThree; cout<<endl; return 0; }
Here is its sample run with user input, codes, and cracker as first and second strings:
Note: The previous program will only work with string input without any spaces.
String Concatenation using a Pointer in C++
This is the last program that concatenates two strings using pointers.
#include<iostream> #include<stdio.h> using namespace std; int main() { char strOne[100], strTwo[50]; char *firstString, *secondString; cout<<"Enter the First String: "; gets(strOne); cout<<"Enter the Second String: "; gets(strTwo); firstString = strOne; secondString = strTwo; while(*firstString) firstString++; while(*secondString) { *firstString = *secondString; firstString++; secondString++; } *firstString = '\0'; cout<<"\nString after Concatenation: "<<strOne; cout<<endl; return 0; }
Here's an example of how it works with user input, C++ Program to as the first string, and Concatenate Two Strings as the second string:
The address of both the string strOne and strTwo gets initialized to its corresponding pointers, say firstString and secondString. And based on the address, we've concatenated the second string's value to the first one.
Note: The * is called as the value at operator.
The same program in different languages
« Previous Program Next Program »