
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to get length of strings, perform concatenation and swap characters
Suppose we have two strings s and t, we shall have to find output in three lines, the first line contains lengths of s and t separated by spaces, the second line is holding concatenation of s and t, and third line contains s and t separated by space but their first characters are swapped.
So, if the input is like s = "hello", t = "programmer", then the output will be
5 10 helloprogrammer pello hrogrammer
To solve this, we will follow these steps −
display length of s then print one space and length of t
display s + t
temp := s[0]
s[0] := t[0]
t[0] := temp
display s then one blank space and display t
Example
Let us see the following implementation to get better understanding −
#include <iostream> using namespace std; int main(){ string s = "hello", t = "programmer"; cout << s.length() << " " << t.length() << endl; cout << s + t << endl; char temp = s[0]; s[0] = t[0]; t[0] = temp; cout << s << " " << t << endl; }
Input
"hello", "programmer"
Output
5 10 helloprogrammer pello hrogrammer
- Related Articles
- C program to swap two strings
- Java Program to Swap Pair of Characters
- Concatenation of two strings in PHP program
- C function to Swap strings
- Program to equal two strings of same length by swapping characters in Python
- C program to print characters and strings in different formats.
- Program to swap string characters pairwise in Python
- Order strings by length of characters IN mYsql?
- Program to get maximum length merge of two given strings in Python
- Java program to swap first and last characters of words in a sentence
- Concatenation of Strings in Java
- C++ program to find uncommon characters in two given strings
- Find uncommon characters of the two strings in C++ Program
- Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript
- Smart concatenation of strings in JavaScript

Advertisements