VOOZH about

URL: https://www.geeksforgeeks.org/cpp/reverse-prefix-of-word-in-cpp/

⇱ Reverse Prefix of Word in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reverse Prefix of Word in C++

Last Updated : 23 Jul, 2025

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 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 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++.


Output
Resulting 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++.


Output
Resulting 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.

Comment
Article Tags: