-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Monday 6 July 2015

Concatenation of strings in C++


Today I am here with a program that will perform the operation of concatenation of strings of addition of strings.

Concatenation of strings

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. 

For example,"Hello" is string # 1 and "World" is string # 2.So,after concatenation,the result will be "Hello World".

Below is the source code of this program.

Source code

#include <iostream>
using namespace std;
int main()
{
    char s1[100], s2[100];
    int i, j;
    cout << "Enter first string: ";
    cin >> s1;
    cout << "Enter second string: ";
    cin >> s2;
    for(i=0; s1[i]!='\0'; ++i);  /* i contains length of string s1. */
    for(j=0; s2[j]!='\0'; ++j, ++i) //it adds two strings
    {
        s1[i]=s2[j];
    }
    s1[i]='\0'; 
    cout << "After concatenation: " << s1;
    return 0;
}

The reference of this program is taken from Concatenation.

Output

concatenation of strings

No comments:

Post a Comment