
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Get Length of Strings, Perform Concatenation and Swap Characters in C++
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
Advertisements