Reverse Prefix of Word in C++
Last Updated :
20 Aug, 2024
Reversing a prefix of a word is a problem in which we are given a word and a character ch, we need to reverse the segment of the word that starts at the beginning of the word and ends at the first occurrence of ch (inclusive). If ch is not present in the word, the word should remain unchanged.
Example:
Input:
word = "geeksforgeeks"
ch = 's'
Output:
"skeegforgeeks"
Explanation:
In the example above, the prefix of the word "geeksforgeeks" up to the first occurrence of 's' is "geeks".
Reversing this prefix results in "skeeg", and the remaining part of the word "forgeeks" is left
unchanged, giving us the final result "skeegforgeeks".
Ways to Reverse Prefix of a Word in C++
There are several efficient ways to reverse the prefix of a word in C++. Below, we'll explore two approaches to achieve this.
1. Using std::reverse and std::find
In this approach, we can use the std::find function to locate the first occurrence of the specified character ch in the word. Once we find the position of ch, we can reverse the substring from the beginning of the word up to that position using the std::reverse function.
Approach:
- Use std::find to locate the position of the character ch.
- If ch is found, use std::reverse to reverse the substring from the beginning of the word up to the position of ch.
- If ch is not found, return the word as it is.
Example:
The below example demonstrates how we can use the std::reverse and std::find functions to reverse prefix of a word in C++.
C++
// C++ program to reverse prefix of a word using std::reverse and std::find
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
string reversePrefix(string word, char ch)
{
// Find the first occurrence of the character ch
auto pos = find(word.begin(), word.end(), ch);
// If ch is found, reverse the prefix up to the found position
if (pos != word.end())
{
reverse(word.begin(), pos + 1);
}
return word;
}
int main()
{
string word = "geeksforgeeks";
char ch = 's';
string result = reversePrefix(word, ch);
cout << "Resulting word after reversing prefix: " << result << endl;
return 0;
}
OutputResulting word after reversing prefix: skeegforgeeks
Time Complexity: The time complexity of this approach is O(n), where n is the length of the word. This is because std::find and std::reverse each traverse the word at most once.
Auxiliary Space: The space complexity is O(1)as no additional space proportional to the input size is used.
2. Using a Custom Loop
Another approach to reverse the prefix of the word is using a custom loop to directly swap characters in the prefix up to the first occurrence of ch. This approach avoids the use of any additional STL functions beyond basic operations.
Approach:
- Iterate through the word to find the index of the first occurrence of ch.
- Once the character is found, swap elements from the beginning of the word up to the index of ch in a custom loop.
- If ch is not found, return the word as it is.
Example:
The below example demonstrates how we can use the loop to reverse prefix of a word in C++.
C++
// C++ program to reverse prefix of a word using a custom loop
#include <iostream>
#include <string>
using namespace std;
string reversePrefix(string word, char ch){
// Find the index of the first occurrence of ch
int index = -1;
for (int i = 0; i < word.size(); i++){
if (word[i] == ch){
index = i;
break;
}
}
// If ch is found, reverse the prefix up to the found index
if (index != -1){
int left = 0, right = index;
while (left < right){
swap(word[left], word[right]);
left++;
right--;
}
}
return word;
}
int main()
{
string word = "abcdefd";
char ch = 'd';
string result = reversePrefix(word, ch);
cout << "Resulting word after reversing prefix: " << result << endl;
return 0;
}
OutputResulting word after reversing prefix: dcbaefd
Time Complexity: The time complexity is O(n), where nis the length of the word, since the loop iterates through the word at most twice.
Auiliary Space: The space complexity is O(1), as no additional space proportional to the input size is required.
Similar Reads
Reverse middle words of a string Given a string str, print reverse all words except the first and last words. Examples: Input : Hi how are you geeks Output : Hi woh era uoy geeks Input : I am fine Output : I ma finePrint the first word.For remaining middle words, print the reverse of every word after reaching end of it. This will p
4 min read
C++ Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p
7 min read
How to Reverse a Deque in C++? In C++ STL, we have a container called deque(short for double-ended queue) that allows fast insertion and deletion operations at both the beginning and end. In this article, we will learn how to reverse a deque in C++. Example: Input: myDeque = {1, 2, 3, 4, 5}; Output: Reversed Deque: 5 4 3 2 1Rever
2 min read
Reverse a Number in C++ In this article, we will learn to write a C++ program to reverse a number. Reversing the digits of a number means changing the order of the digits of the number so that the last digit becomes the first digit, the second last digit becomes the second digit, and so on. The number upon reversing read t
2 min read
Program to reverse words in a given string in C++ Given a sentence in the form of string str, the task is to reverse each word of the given sentence in C++. Examples: Input: str = "the sky is blue" Output: blue is sky theInput: str = "I love programming" Output: programming love I Method 1: Using STL functions Reverse the given string str using STL
6 min read