Replace all the vowels of the given input string with a single digit number.
Input:
String of length L and all lower-case letters.
Output:
Input String replaced with the digit.
Program explanation:
Replace all the vowels in the given input string with a single digit number you get after all the below
steps.
Step 1: Get the index of the vowel in the given input.
Step 2: Multiply the index with 100.
Step 3: Sum all the prime numbers between 1 and the resulting number from last step.
Step 4: Add the digits of the number you get from the last step until you get a single digit.
Step 5: Now, Replace the vowel with the digit from the last step.
Question 1 – Replace Vowels with a digit
Blackbuck Insights Private Limited Confidential and Proprietary
P a g e | 2
Example 1:
Input:
hello
Output:
h7ll9
Explanation:
Step 1: First vowel found at index 1.
Step 2: 1 * 100 = 100
Step 3: Sum of prime numbers between 1 and 100 is 1060
Step 4: Sum of each digit until we get single digit:
= 1 + 0 + 6 + 0
= 7
Step 5: Replace e with 7
Output now: h7llo
Step 1: Next vowel found at index 4.
Step 2: 4 * 100 = 400
Step 3: Sum of prime numbers between 1 and 400 is 13887
Step 4: Sum of each digit until we get single digit:
= 1 + 3 + 8 + 8 + 7
= 27
= 2 + 7
= 9
Step 5: Replace o with 9
Final output: h7ll9
Example 2:
Input : replace this
Output. : r7pl9c1 th5s
Answers
Program in Java:
import java.util.*;
public class MyClass
{
static boolean prime(int n)
{
int flag = 0;
for(int i = 2 ; i < n ; i++)
{
if(n % i == 0)
{
flag = 1;
}
}
if(flag == 1)
{
return false;
}
else
{
return true;
}
}
public static void main(String args[])
{
Scanner Sc = new Scanner(System.in);
String s, new_string = "";
System.out.print("Enter a string : ");
s = Sc.nextLine();
int len = s.length();
s = s.toLowerCase();
for(int i = 0 ; i < len ; i++)
{
char ch = s.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
int mul = i * 100;
int sum = 0;
for(int j = 2 ; j <= mul ; j++)
{
if(prime(j))
{
sum = sum + j;
}
}
while(sum > 9)
{
int dig = 0;
while(sum != 0)
{
dig = dig + sum%10;
sum = sum/10;
}
sum = dig;
}
new_string = new_string + sum;
}
else
{
new_string = new_string + ch;
}
}
System.out.print("New string : " + new_string);
}
}
Output 1:
Enter a string : hello
New string : h7ll9
Output 2:
Enter a string : replace this
New string : r7pl9c1 th5s