Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Hashtable;
- public class StringLib {
- public static Character firstNonRepeated(String str){
- int[] strA=new int[128];
- for (int i=0; i<strA.length;i++){
- strA[i]=0;
- }
- for (char c: str.toCharArray()){
- strA[c]++;
- }
- for (char c: str.toCharArray()){
- if (strA[c]==1)
- return new Character(c);
- }
- return null;
- }
- public static Character firstNonRepeated2(String str){
- Hashtable<Character,Integer> hash=new Hashtable<Character,Integer>();
- Character ch;
- Integer intgr;
- for (char c: str.toCharArray()){
- ch=new Character(c);
- intgr=(Integer)hash.get(ch);
- if (intgr==null)
- hash.put(ch, new Integer(1));
- else
- hash.put(ch, new Integer(intgr.intValue()+1));
- }
- for (char c: str.toCharArray()){
- ch=new Character(c);
- intgr=(Integer)hash.get(ch);
- if (intgr==1)
- return ch;
- }
- return null;
- }
- public static void main(String[] args){
- String str="long string with all sorts of punctuation! hello there ; ?";
- System.out.println("Fcn1: The first nonrepeated character of "+ str+" is: "+ StringLib.firstNonRepeated(str));
- System.out.println("Fcn2: The first nonrepeated character of "+ str+" is: "+ StringLib.firstNonRepeated2(str));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement