
- 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
Create a new string by alternately combining the characters of two halves of the string in reverse in C++ Program
In this tutorial, we are going to write a program that creates a new string by alternately combining the characters of the two halves of the string in reverse order.
Let's see the steps to solve the problem.
Initialize the string.
Find the length of the string.
Store the first half and second half string indexes.
Iterate from the ending of the two halves of the string.
Add each character to the new string.
Print the new string.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void getANewString(string str) { int str_length = str.length(); int first_half_index = str_length / 2, second_half_index = str_length; string new_string = ""; while (first_half_index > 0 && second_half_index > str_length / 2) { new_string += str[first_half_index - 1]; first_half_index--; new_string += str[second_half_index - 1]; second_half_index--; } if (second_half_index > str_length / 2) { new_string += str[second_half_index - 1]; second_half_index--; } cout << new_string << endl; } int main() { string str = "tutorialspoints"; getANewString(str); return 0; }
Output
If you execute the above program, then you will get the following result.
asitrnoitouptsl
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Python program to check if both halves of the string have same set of characters.
- Check if both halves of the string have same set of characters in Python
- Check if both halves of the string have same set of characters in C#
- Check if both halves of the string have the same set of characters in Python
- Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String
- Reverse the words in the string that have an odd number of characters in JavaScript
- How to insert new string within a string subsequent to removing the characters from the original string by using MySQL function?
- PHP program to find the number of characters in the string
- Program to reverse the position of each word of a given string in Python
- Reverse Vowels of a String in Python
- Reverse Vowels of a string in C++
- Program to check whether String Halves Are Alike in Python
- Python Program to Remove the Characters of Odd Index Values in a String
- Count Number of Lowercase Characters in a String in Python Program
- Java Program to Find the Duplicate Characters in a String

Advertisements