public class Demo
{
public void vConvertMsg(String sInputStr, StringBuffer OutputStr)
{
if(null == OutputStr)return;
//Code Here
System.out.println("sInputStr:"+sInputStr);
if(sInputStr.equals("")){
OutputStr.append("");
return;
}
char[] words = sInputStr.toCharArray();
Stack stack = new Stack(words.length+1);
stack.push('\0');
for(Character word:words){
if(Character.isLetter(word)|| word == '-'){
// if(!Character.isWhitespace(word)&&!Character.isDigit(word)){
if(!stack.isFull()){
stack.push(word);
}
}else{
if(stack.getElementCount()>0){
char[] tmp =new char[stack.getElementCount()];
for(int i=0;i<tmp.length;i++){
if(!stack.isEmpty()){
try {
tmp[i] = stack.pop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
OutputStr.append(tmp);
if(Character.isWhitespace(word)){
OutputStr.append(" ");
}else{
OutputStr.append(word);
}
}else{
OutputStr.append(word);
}
}
}
}
}
java实现的stack
/**
* Implement a stack with array.
* function:
*
*/
public class Stack {
char[] data;
int maxSize;
//top of the stack
int top;
public Stack(int maxSize) {
this.maxSize = maxSize;
data = new char[maxSize];
top = -1;
}
/**
* get the Size of the stack
* @return maxSize
*/
public int getSize()
{
return maxSize;
}
/**
* return the number of the elements of the stack,
*
* BE CAREFUL:
* it is not correct when you first calculate, because top = -1;
* @return top
*/
public int getElementCount()
{
return top;
}
/**
* Determine the stack is empty
* @return
*/
public boolean isEmpty()
{
return top == -1;
}
public void setEmpty(){
top = -1;
}
/**
* Determine the stack is full
* @return
*/
public boolean isFull()
{
return top+1 == maxSize;
}
/**
* push the data
* @param data
* @return true when push successful,otherwise if false
*/
public boolean push(char data) {
if(isFull())
{
System.out.println("The stack is full.");
return false;
}
this.data[++top] = data;
return true;
}
/**
* get data from the stack
* @return get data
*/
public char pop() throws Exception{
if(isEmpty())
{
throw new Exception("The stack is empty.");
}
return this.data[top--];
}
/**
* Back to the top of the stack elements
* @return
*/
public char peek()
{
return this.data[getElementCount()];
}
}