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

import java.util.*; import java.io.*; public class ReverseStack { public static

ID: 663346 • Letter: I

Question

import java.util.*;
import java.io.*;
public class ReverseStack
{
   public static void main(String[] args)
   {
Scanner scan = new Scanner(System.in);
String str;
  
System.out.println(" Enter the String to reverse: ");
str = scan.nextLine();
String[] line;
line = str.split("\.");
System.out.println("The reversed String is: ");
for (int i = 0; i <line.length; i++)
{
   stringRev(line[i].trim());
   System.out.print("");
}
   }
public static void stringRev(String OrgString)
   {
Stack <String> Stack1 = new Stack <String>();
String[] temp1;
String delimit1 = " ";
temp1 = OrgString.split(delimit1);
for (int i = 0; i < temp1.length; i++)
{
   Stack1.push(temp1[i].toLowerCase());
}
String str = "";
while(!Stack1.empty()){
   str = str + " " + Stack1.pop();
}
str = str + ".";
str = str.replaceFirst("^*","");
String str1 = Character.toUpperCase(str.charAt(0)) + str.substring(1);
System.out.print(str1);
   }
}

I am having problems with my code. The capitalization and placement of the period is messed up

JAVA: Use a stack to reverse the words of a sentence. Keep reading words until you have a word that ends in a period, adding them onto a stack. When you have a word with a period, pop the words off and print them. Stop when there are no more words in the input. For example, you should turn the input into:

Mary had a little lamb. Its fleece was white as snow.

into

Lamb little a had mary. Snow as white was fleece its.

Explanation / Answer

I have mentioned Reverse words function in the main function only.I did not define it separately as u did.But, it will not effect i wil do separately also.You can see the below code for reversing words in the given sentence and it will check the delimiters also as mentioned in the above question.

import java.util.*;
public class ReverseStack
{
public static void main(String[] args) {
  
String a = "Mary had a little lamb. Its fleece was white as snow";
Stack <String> stack = new Stack <String>();
String[] temp;
String delimiter = " ";

temp = a.split(delimiter);
// push substring to stack
for(int i =0; i < temp.length ; i++)
{
stack.push(temp[i]);
}
System.out.println(" Original string: " + a);
System.out.print("Reverse word string: ");
while(!stack.empty()) {
System.out.print(stack.pop());
System.out.print(" ");
}
System.out.println(" ");
}
}