Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote