0

How could I find a letter in string (if it's "cc" together it leaves it, but if it's only one "c" it deletes it)?

I have no idea how to do it, so if someone knows I would be really thankful.

I tried splitting whole string into a tab, and checking if index is "c" and then looking +-1 index, to check if "c" is next to it but it didn't work.

Now i changed "==" but it still misses some letters. if i input "abaabbcbaccb" it should output →"aabaaaabbaaccb" My program misses that standalone "c"..

public class NovNiz{
public static String urejanjeBesedila(String a){
    String niz="";
    a.toLowerCase();
    String spr = a.replaceAll("a", "aa");
    spr = spr.toLowerCase();
    spr = spr.replaceAll("bb", "b");
    String []tab=spr.split(" ");
    if(tab.length>0){
        for(int i=0;i<tab.length;i++){
            if(tab[i].indexOf("cc")>0){
                niz=niz+tab[i]+" ";
                continue;           

            }
            if(tab[i].indexOf("c")>=0){
                    int x=tab[i].indexOf("c");
                    StringBuffer sb= new StringBuffer(tab[i]);
                    sb.deleteCharAt(x);
                    tab[i]=sb.toString();
                    }
            niz=niz+tab[i]+" ";
        }
    }
    else{
        String []tab2=spr.split("");
            for(int i=0;i<tab.length;i++){
                if(i==0){
                    if("c".equals(tab[i])&&"c".equals(tab[i+1])){

                    }
                    else if("c".equals(tab[i])){
                        tab[i]="";
                    }
                }
                else{
                if("c".equals(tab[i])&&"c".equals(tab[i+1])||"c".equals(tab[i-1])){

                    }
                else if("c".equals(tab[i])){
                    tab[i]="";
                    }
                }
            }
        }
    return niz;
}   

}

ScaredFace
  • 25
  • 3
  • 2
    `tab[i]=="c"` is not how you compare `String`s in Java, instead you should use something like `"c".equals(tab[i])` – MadProgrammer Jan 03 '16 at 10:14
  • 1
    `spr.toLowerCase();` is not how you turn all the character's of spr to lower case. Instead you should use something like `spr = spr.toLowerCase();` – SMA Jan 03 '16 at 10:16
  • 1
    Oh, and `String` is immutable in Java – MadProgrammer Jan 03 '16 at 10:16
  • Maybe you can try regular expressions https://docs.oracle.com/javase/tutorial/essential/regex/intro.html – Shahin Jan 03 '16 at 10:37
  • 1
    You should check out `String.equalsIgnoreCase(String anotherString)`. – MeetTitan Jan 03 '16 at 10:59
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –  Jan 03 '16 at 11:04

1 Answers1

1

You can just do:

final String input = "I'm a test string to drop 'c' but not 'cc', 'ccc', 'cccc', ok ?";

String result = input
                    .replaceAll( "c([^c])", "$1" )
                    .replaceAll( "c([^c])", "cc$1" );

// result will be "I'm a test string to drop '' but not 'cc', 'ccc', 'cccc', ok ?"
Prim
  • 2,880
  • 2
  • 15
  • 29
  • But could you explain that regex? ( "c([^c])", "$1" ) ( "c([^c])", "cc$1" ) – ScaredFace Jan 03 '16 at 11:51
  • The first one expression catches c followed by a letter which is not a c, and replaces these both letters by the letter which is not a c ($1 refers to the fisrt group between parenthesis in expression). Doing that is like removing one c for each sequence of c. Then, the second one re-adds a c for each remaining sequence of c to complete. – Prim Jan 03 '16 at 12:05