Java code. I\'m looking for method that uses Stack to convert the passed string
ID: 3710328 • Letter: J
Question
Java code. I'm looking for method that uses Stack to convert the passed string (case insensitive) to uppercase form. For example:
"Wooorrrrrllld" without quotes, it should return "WO3R5L3D".
"Maaaaaanny" without qutes, it should return "MA6N2Y"
If it's only one character, do not return the number 1 next to it.
For a starting method please use "public static String myFunction(String myString)".
myString is a String that is passed in.
The return should return the string with the number of occurences as the examples above.
However, if the string passed in is null or empty, it should return null.
Explanation / Answer
import java.util.*;
public class Main {
public static void myFunction(String str) {
char[] newStr=str.toCharArray();
int count;
for(int i = 0; i<newStr.length; i++) {
count = 1;
while(newStr[i] == newStr[i+1]) {
count++;
i++;
}
if(count > 1) {
System.out.print(newStr[i] + count);
} else {
System.out.print(newStr[i]);
}
if(i==newStr.length)break;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string: ");
String str=sc.next();
myFunction(str);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.