华为过滤字符串(java)

/*
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。
比如字符串“abacacde”过滤结果为“abcde”。
要求实现函数: 
void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr:  输入字符串
         lInputLen:  输入字符串长度         
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例 
输入:“deefd”        输出:“def”
输入:“afafafaf”     输出:“af”
输入:“pppppppp”     输出:“p”

*/

 1 package 华为机试;
 2 
 3 
 4 import java.util.Scanner;
 5 
 6 
 7 public class Main2 {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         Scanner scn=new Scanner(System.in);
12         String s=scn.next();
13         char c[]=s.toCharArray();
14         boolean exit[]=new boolean[26];
15         int index=0;
16         
17         for(int i=0;i<c.length;i++)
18         {
19             if(!exit[c[i]-'a'])
20             {
21                 exit[c[i]-'a']=true;
22                 c[index++]=c[i];
23             }
24             
25         }
26         System.out.println(new String(c).substring(0,index));
27     }
28 
29 }

 

posted @ 2014-08-30 21:54  hansongjiang8  阅读(423)  评论(0编辑  收藏  举报