|
-
Feb 4th, 2007, 02:13 AM
#1
Thread Starter
Lively Member
remove duplicate characters
for the string "an example", i want to remove the dulipcate character from that string. and how do it remove the spaces between the words?
in the end i should get:
"anexmpl".
thanks.
-
Feb 4th, 2007, 03:52 AM
#2
Addicted Member
Re: remove duplicate characters
removing spaces is as simple as
Code:
words = words.replaceAll(" ", "");
If noone else gets the other part before i get up tommarrow I will help you out but i gotta sleep now.
-
Feb 4th, 2007, 07:00 AM
#3
Thread Starter
Lively Member
Re: remove duplicate characters
so how to remove duplicate character. i need that urgently.
thanks.
-
Feb 4th, 2007, 01:35 PM
#4
Addicted Member
Re: remove duplicate characters
Ok, there are a few different ways this can be done, heres what I did:
Code:
import java.util.*;
public class DublicateRemoval
{
public static void main (String[] args)
{
Scanner myInput = new Scanner(System.in);
String words = myInput.nextLine();
//removing spaces
words = words.replaceAll(" ", "");
//replace duplicates
String temp = "";
int position = 0;
while(position < words.length())
{
temp = words.substring(position + 1);
temp = temp.replaceAll(words.charAt(position) + "", "");
//System.out.println(temp);
//System.out.println(words.substring(0, position+1));
words = words.substring(0, position + 1) + temp;
position ++;
}
System.out.println(words);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|