public class ReverseStack { private int max; private String[] stArr; private int
ID: 663182 • Letter: P
Question
public class ReverseStack
{
private int max;
private String[] stArr;
private int t;
public Capitalization (int n) {
max = n;
stArr = new String[max];
t = -1;
}
/***
* Method to push elements in stack
* @param j
*/
public void push(String j) {
stArr[++t] = j;
}
/***
* Method to pop elements from stack;
* @return
*/
public String pop() {
return stArr[t--];
}
/***
* Method to check if stack is empty
* @return
*/
public boolean isEmpty() {
return (t == -1);
}
public static void main(String[] args) {
String s = "Mary had a little lamb. Its fleece was white as snow.";
String[] w = s.split("\s");
Capitalization stackImplement = new Capitalization(w.length);
int x=0;
for(int i=0;i<w.length;i++)
{
if(w[i].contains("."))
{
w[i]=w[i].replaceAll("\.",""); // removing and replacing period(.) with null.
stackImplement.push(w[i]); // pushing element having period with it.
//Popping from stack. Checking conditions of capital letters.
while (!stackImplement.isEmpty()) {
System.out.print(" ");
String value = stackImplement.pop();
if(x==0)
{
StringBuffer res = new StringBuffer();
char[] stringArray = value.trim().toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
value = new String(stringArray);
res.append(value).append(" ");
x++;
System.out.print(value);
}
else
{
System.out.print(value.toLowerCase());
}
}
System.out.print(".");
x=0;
}
else
{
stackImplement.push(w[i]); // pushing elements to stack.
}
}
}
}
This is 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
Pay attention to capitalization and the placement of the period.
It is not working
Code:
Mary had a little lamb. Its fleece was white as snow.
Explanation / Answer
public class ReverseStack
{
private int max;
private String[] stArr;
private int t;
public ReverseStack (int n) {
max = n;
stArr = new String[max];
t = -1;
}
/***
* Method to push elements in stack
* @param j
*/
public void push(String j) {
stArr[++t] = j;
}
/***
* Method to pop elements from stack;
* @return
*/
public String pop() {
return stArr[t--];
}
/***
* Method to check if stack is empty
* @return
*/
public boolean isEmpty() {
return (t == -1);
}
public static void main(String[] args) {
String s = "Mary had a little lamb. Its fleece was white as snow.";
String[] w = s.split("\s");
ReverseStack stackImplement = new ReverseStack(w.length);
int x=0;
for(int i=0;i<w.length;i++)
{
if(w[i].contains("."))
{
w[i]=w[i].replaceAll("\.",""); // removing and replacing period(.) with null.
stackImplement.push(w[i]); // pushing element having period with it.
//Popping from stack. Checking conditions of capital letters.
while (!stackImplement.isEmpty()) {
System.out.print(" ");
String value = stackImplement.pop();
if(x==0)
{
StringBuffer res = new StringBuffer();
char[] stringArray = value.trim().toCharArray();
stringArray[0] = Character.toUpperCase(stringArray[0]);
value = new String(stringArray);
res.append(value).append(" ");
x++;
System.out.print(value);
}
else
{
System.out.print(value.toLowerCase());
}
}
System.out.print(".");
x=0;
}
else
{
stackImplement.push(w[i]); // pushing elements to stack.
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.