Reverse the string whenever digit is encountered Last Updated : 19 May, 2024 Comments Improve Suggest changes Like Article Like Report Given a string s of length N, the task is to traverse the string and reverse the substring from the start to the next encountered digit. After reversing the substring, update the start index to the character immediately following the digit. Example: Input: s = abc123def456ghiOutput: cba123fed456ihg Input: s = abc156ghiOutput: cba156ihg Approach: The key idea is to identify these substrings and reverse them individually without affecting the digits. We achieve this by iterating through the string, finding the next digit, and reversing the substring before it. Step-by-Step Approach: Initialize start and end indices to 0:These mark the beginning and end of substrings to be reversed.Loop through the string:While end is less than the string length:Find the next digit's position.Reverse the substring from start to end-1Below is the implementation of the above approach: C++ #include <bits/stdc++.h> using namespace std; string reverseSubstrings(string s) { int start = 0; int end = 0; // Loop through the string while (end < s.length()) { // Find the next digit while (end < s.length() && !isdigit(s[end])) { end++; } // Reverse the substring from start to end-1 reverse(s.begin() + start, s.begin() + end); // Update the start index to the character // immediately following the digit start = end + 1; end = start; } return s; } int main() { string s = "abc123def456ghi"; string reversed = reverseSubstrings(s); cout << "Original string: " << s << endl; cout << "Reversed string: " << reversed << endl; return 0; } Java public class ReverseSubstrings { public static String reverseSubstrings(String s) { StringBuilder sb = new StringBuilder(s); int start = 0; int end = 0; // Loop through the string while (end < sb.length()) { // Find the next digit while (end < sb.length() && !Character.isDigit(sb.charAt(end))) { end++; } // Reverse the substring from start to end-1 sb.replace( start, end, new StringBuilder(sb.substring(start, end)) .reverse() .toString()); // Update the start index to the character // immediately following the digit start = end + 1; end = start; } return sb.toString(); } public static void main(String[] args) { String s = "abc123def456ghi"; String reversed = reverseSubstrings(s); System.out.println("Original string: " + s); System.out.println("Reversed string: " + reversed); } } // This code is contributed by shivamgupta0987654321 Python def reverse_substrings(s): start = 0 end = 0 length = len(s) # Loop through the string while end < length: # Find the next digit while end < length and not s[end].isdigit(): end += 1 # Reverse the substring from start to end-1 s = s[:start] + s[start:end][::-1] + s[end:] # Update the start index to the character # immediately following the digit start = end + 1 end = start return s def main(): s = "abc123def456ghi" reversed_s = reverse_substrings(s) print("Original string:", s) print("Reversed string:", reversed_s) if __name__ == "__main__": main() # This code is contributed by Ayush Mishra JavaScript function reverseSubstrings(s) { let sb = s.split(''); let start = 0; let end = 0; // Loop through the string while (end < sb.length) { // Find the next digit while (end < sb.length && isNaN(parseInt(sb[end]))) { end++; } // Reverse the substring from start to end-1 sb.splice( start, end - start, sb.slice(start, end).reverse().join('')); // Update the start index to the character // immediately following the digit start = end + 1; end = start; } return sb.join(''); } function main() { let s = "abc123def456ghi"; let reversed = reverseSubstrings(s); console.log("Original string: " + s); console.log("Reversed string: " + reversed); } // Call the main function main(); OutputOriginal string: abc123def456ghi Reversed string: cba123fed456ihg Time complexity: O(n) where n is the length of the input string.Auxiliary space: O(n) where n is the length of the input string. Comment More infoAdvertise with us Next Article How to reverse a string in R P pantharshx9d9 Follow Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads Reverse the given string in the range [L, R] Given a string str, and two integers L and R, the task is to reverse the string in the range [L, R] i.e. str[L...R].Examples: Input: str = "geeksforgeeks", L = 5, R = 7 Output: geeksrofgeeks Reverse the characters in the range str[5...7] = "geeksforgeeks" and the new string will be "geeksrofgeeks" I 4 min read Reverse String according to the number of words Given a string containing a number of words. If the count of words in string is even then reverse its even position's words else reverse its odd position, push reversed words at the starting of a new string and append the remaining words as it is in order. Examples: Input: Ashish Yadav Abhishek Rajp 6 min read Easiest Way to Reverse a String Have you wondered which is the easiest way to reverse a string, Imagine you are giving a contest and an algorithm you have made requires reversing a string, you must be aware of the easiest way to reverse a string. What is a reversal of string:Reversing a string is the technique that reverses or cha 4 min read How to reverse a string in R Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a 4 min read How to reverse a string in R Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a 4 min read Write a program to reverse digits of a number Given an Integer n, find the reverse of its digits.Examples: Input: n = 122Output: 221Explanation: By reversing the digits of number, number will change into 221.Input: n = 200Output: 2Explanation: By reversing the digits of number, number will change into 2.Input: n = 12345 Output: 54321Explanation 8 min read Like