Replace every consonant sequence with its length in the given string
Given a string str consisting of lowercase characters. The task is to replace the consecutive sequence of consonants with their length.
Examples:
Input: str = “abcdeiop”
Output: a3eio1
Given string contains 2 consonant sequences “bcd” and “p” with lengths 3 and 1.
Input: str = “abecidofu”
Output: a1e1i1o1u
Approach: Make a new string that will be free from consonants. For it, we have to iterate the given string. If we find a vowel it will be added to the new string as it is. If we find a consonant then we have to find the length of the complete consonant sequence and then add its length to the new string.
Algorithm:
Step 1: Start
Step 2: create a function that returns the string value that replaces every consonant sequence with its length and takes the input of a string type.
Step 3: Initialize an empty string say “res” inside it.
Step 4: To count the length of the consonant sequence as you iterate through the string, define the integer variables I and count.
Step 5: start a while loop to traverse through the input string
Step 6: If the character is a consonant, increment the count by 1.
If the character is a vowel, add the count to the resultant string res, then add the vowel to res, and reset the count to 0.
Step 7: now after the while loop checks if there is a consonant sequence left in the string. If yes, add the count to the resultant string.
Step 8: Return the obtained string res
Step 9: End
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string replaceConsonants(string str)
{
string res = "" ;
int i = 0, count = 0;
while (i < str.length()) {
if (str[i] != 'a'
&& str[i] != 'e'
&& str[i] != 'i'
&& str[i] != 'o'
&& str[i] != 'u' ) {
i++;
count++;
}
else {
if (count > 0)
res += to_string(count);
res += str[i];
i++;
count = 0;
}
}
if (count > 0)
res += to_string(count);
return res;
}
int main()
{
string str = "abcdeiop" ;
cout << replaceConsonants(str);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static String replaceConsonants(String str)
{
String res = "" ;
int i = 0 , count = 0 ;
while (i < str.length()) {
if (str.charAt(i) != 'a'
&& str.charAt(i) != 'e'
&& str.charAt(i) != 'i'
&& str.charAt(i) != 'o'
&& str.charAt(i) != 'u' ) {
i++;
count++;
}
else {
if (count > 0 )
res += count;
res += str.charAt(i);
i++;
count = 0 ;
}
}
if (count > 0 )
res += count;
return res;
}
public static void main(String[] args)
{
String str = "abcdeiop" ;
System.out.println(replaceConsonants(str));
}
}
|
Python3
def replaceConsonants(string) :
res = "";
i = 0 ; count = 0 ;
while (i < len (string)) :
if (string[i] ! = 'a'
and string[i] ! = 'e'
and string[i] ! = 'i'
and string[i] ! = 'o'
and string[i] ! = 'u' ) :
i + = 1 ;
count + = 1 ;
else :
if (count > 0 ) :
res + = str (count);
res + = string[i];
i + = 1
count = 0 ;
if (count > 0 ) :
res + = str (count);
return res;
if __name__ = = "__main__" :
string = "abcdeiop" ;
print (replaceConsonants(string));
|
C#
using System;
public class GFG {
public static string replaceConsonants( string str)
{
string res = "" ;
int i = 0, count = 0;
while (i < str.Length) {
if (str[i] != 'a'
&& str[i] != 'e'
&& str[i] != 'i'
&& str[i] != 'o'
&& str[i] != 'u' ) {
i++;
count++;
}
else {
if (count > 0) {
res += count;
}
res += str[i];
i++;
count = 0;
}
}
if (count > 0) {
res += count;
}
return res;
}
public static void Main( string [] args)
{
string str = "abcdeiop" ;
Console.WriteLine(replaceConsonants(str));
}
}
|
Javascript
<script>
function replaceConsonants(str) {
var res = "" ;
var i = 0,
count = 0;
while (i < str.length) {
if (
str[i] !== "a" &&
str[i] !== "e" &&
str[i] !== "i" &&
str[i] !== "o" &&
str[i] !== "u"
) {
i++;
count++;
} else {
if (count > 0) res += count.toString();
res += str[i];
i++;
count = 0;
}
}
if (count > 0) res += count.toString();
return res;
}
var str = "abcdeiop" ;
document.write(replaceConsonants(str));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n), where n is the length of the given string.
Last Updated :
27 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...