2

im asked to write a program that removes duplicate letters from a string **note: uppercase and lowercase letters are considered duplicates. I wrote the code and it's working for all inputs without spacebars, when an string is given with spaces, it show errors. i have to use loops and arrays only, no extra functions or hashs, this is my code that ALMOST works:

case 2:
    System.out.println("Give the string input");
    String original=reader.next();
    char[] charts=original.toCharArray();
    int length=charts.length;
    for (int i=0; i<length; i++){
        for (int j=i+1; j<length; j++){
            if(charts[i]==charts[j]||charts[i]+32==charts[j] ||charts[i]-32==charts[j]){
                int temp=j; //duplicate element index
                for (int k=temp; k<length-1; k++){ //delete shifting elements to left.
                    charts[k]=charts[k+1];
                }//inner inner for
                j--;
                length--; // reduce char array length because we removed a character
            }//if

        }//inner for

    }//for
    String CleanString= new String(charts); //new string without repeated chars
    CleanString=CleanString.substring(0,length); //set its length 
    System.out.println("New str = "+CleanString);
    break;
D. Lawrence
  • 919
  • 1
  • 9
  • 21
WardJub
  • 33
  • 6
  • 1
    can you provide any specific input for which it is failing? – Raj Suvariya Dec 05 '19 at 09:06
  • The problem is that you are using `next()` instead of `nextLine()` which only reads one token. A token is basically text until a space, so a single word. To read the full input, use `nextLine()`. – Zabuzard Dec 05 '19 at 09:26
  • 1
    "when an string is given with spaces, it show errors." - what errors? – DodgyCodeException Dec 05 '19 at 09:30
  • 1
    `if(charts[i]==charts[j]||charts[i]+32==charts[j] ||charts[i]-32==charts[j])` - this will treat some punctuation characters as "the same" as some lowercase letters. Use `Character.toLowerCase(a) == Character.toLowerCase(b) || Character.toUpperCase(a) == Character.toUpperCase(b)` instead. – DodgyCodeException Dec 05 '19 at 09:32
  • You could save yourself one line: `String cleanString = new String(charts, 0, length);`. – DodgyCodeException Dec 05 '19 at 12:00

2 Answers2

3

I recommend you to use Scanner's method nextLine() to read string with spaces and process it with your algorithm

 Scanner scanner = new Scanner(System.in);
 String original = scanner.nextLine();

By the way, if you cannot use regular expressions, you maybe want to use count sort-based approach. Create an array of size equal to maximum size of char. Iterate over string and increment an array element of index X when you meet character value 'X'. Add X to your array of chars. When you meet array[X] >= 1, do not add X to array.

Steyrix
  • 1,604
  • 1
  • 6
  • 15
0

Your code will work fine. Just use this code at third line:

String original="";
original+=reader.nextLine();
Abdul Alim Shakir
  • 821
  • 11
  • 22