------------------------------------------File 1: StringRoutines.java----------------------------------
package org.Solutions;
public class StringRoutines {
void removeArrayByPos(char[] chrArray,int pos,int arrLength){
int i;
for(i=pos;i<arrLength-1;i++){
chrArray[i]=chrArray[i+1];
}
chrArray[i]='\0';//Shrink Array
}
public char[] remDupCharsInString(String str){
char[] chrArray=str.toCharArray();
int arrLength=chrArray.length;
for(int i=0;i<arrLength;i++){
for(int j=i+1;j<arrLength;j++){
if(chrArray[i]==chrArray[j]){
removeArrayByPos(chrArray,j,arrLength);
j=j-1;
arrLength--;//Shrink Array
}
}
}
return chrArray;
}
}
------------------------------------------File 2: MainClass.java----------------------------------
package org.Solutions;
public class MainClass {
public static void main(String[] args) {
StringRoutines sr=new StringRoutines();
String str="STRINGSSTRINGSSSSSSSSSSSSSTRING";
char[] chr=sr.remDupCharsInString(str);
System.out.println("Given String : "+str);
System.out.print("After removing duplicates : ");
System.out.print(chr);
}
}
Output:
-------------------------------------------------------------------
Given String : STRINGSSTRINGSSSSSSSSSSSSSTRING
After removing duplicates : STRING
package org.Solutions;
public class StringRoutines {
void removeArrayByPos(char[] chrArray,int pos,int arrLength){
int i;
for(i=pos;i<arrLength-1;i++){
chrArray[i]=chrArray[i+1];
}
chrArray[i]='\0';//Shrink Array
}
public char[] remDupCharsInString(String str){
char[] chrArray=str.toCharArray();
int arrLength=chrArray.length;
for(int i=0;i<arrLength;i++){
for(int j=i+1;j<arrLength;j++){
if(chrArray[i]==chrArray[j]){
removeArrayByPos(chrArray,j,arrLength);
j=j-1;
arrLength--;//Shrink Array
}
}
}
return chrArray;
}
}
------------------------------------------File 2: MainClass.java----------------------------------
package org.Solutions;
public class MainClass {
public static void main(String[] args) {
StringRoutines sr=new StringRoutines();
String str="STRINGSSTRINGSSSSSSSSSSSSSTRING";
char[] chr=sr.remDupCharsInString(str);
System.out.println("Given String : "+str);
System.out.print("After removing duplicates : ");
System.out.print(chr);
}
}
Output:
-------------------------------------------------------------------
Given String : STRINGSSTRINGSSSSSSSSSSSSSTRING
After removing duplicates : STRING
Comments
Post a Comment