单词倒序,“-”作为单词的一部分,"--"作为和其他特殊字符或数字作为分隔符
如: i am a--stu-dent
输出:stu-dent a am i
我使用的是栈的概念来解决问题
变量命名随便弄的。。。
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String input="@@I--am n-ot*&(under--stand3you";
Stack<Character> charstack=new Stack<>();
for(int i=0;i<input.length();i++)
{
charstack.push(input.charAt(i));
System.out.println(input.charAt(i));
}
System.out.println(charstack.size());
Stack<String> result=new Stack<String>();
String tempstr="";
while(!charstack.isEmpty())
{
char temp=charstack.pop();
System.out.println(temp+"--");
if((temp>='a' && temp<='z') || (temp>='A' && temp<='Z')){
tempstr=temp+tempstr;
}
else if(temp=='-'){
if(charstack.peek()=='-'){
if(!tempstr.equals("")){
result.push(tempstr.trim());
}
if(!result.peek().equals(" ")){
result.push(" ");
}
charstack.pop();
tempstr="";
}else{
tempstr=temp+tempstr;
}
}
else{
if(!tempstr.equals("")){
result.push(tempstr.trim());
}
if(!result.peek().equals(" ")){
result.push(" ");
}
tempstr="";
}
}
result.add(tempstr);
for(int k=0,h=result.size();k<h;k++)
{
System.out.print(result.get(k));
}
}
}