java - Ignoring upper/lowercase strings -
my goal change form of word "java" in sentence "java".i've got done code won't read in mixed cases example:java, java,etc. know suppose use touppercase , tolowercase or equalsignorecase not sure how use properly. not allowed use replace or replace all, teacher wants substring method.
scanner input=new scanner(system.in); system.out.println("enter sentence words including java"); string sentence=input.nextline(); string find="java"; string replace="java"; string result=""; int n; do{ n=sentence.indexof(find); if(n!=-1){ result =sentence.substring(0,n); result=result +replace; result = result + sentence.substring(n+find.length()); sentence=result; } }while(n!=-1); system.out.println(sentence); }
}
you allowed use touppercase()
? try one
scanner input=new scanner(system.in); system.out.println("enter sentence words including java"); string sentence=input.nextline(); string find="java"; string replace="java"; string result=""; result = sentence.tolowercase(); result = result.replace(find,replace); system.out.println(result); }
reply result :))
update : based on
i've got done code won't read in mixed cases example:java, java,etc.
you can use code
scanner input=new scanner(system.in); system.out.println("enter sentence words including java"); string sentence=input.nextline(); string find="java"; string replace="java"; string result=""; int n; do{ //for ignore(converts sentence lowercase) either lower or upper case in sentence nxt process sentence = sentence.tolowercase(); n=sentence.indexof(find); if(n!=-1){ result =sentence.substring(0,n); result=result +replace; result = result + sentence.substring(n+find.length()); sentence=result; } }while(n!=-1); system.out.println(sentence); }
update 2 : put tolowercase convertion outside loop.
public static void main(string[] args){ string sentence = "hello name java im java man java java ice cream"; string find="java"; string replace="java"; string result=""; int n; //for ignore(converts sentence lowercase) either lower or upper case in sentence nxt process sentence = sentence.tolowercase(); system.out.println(sentence); do{ n=sentence.indexof(find); if(n!=-1){ result =sentence.substring(0,n); result=result +replace; result = result + sentence.substring(n+find.length()); sentence=result; } }while(n!=-1); system.out.println(sentence); }
result
hello name java im java man java java ice cream
hello name java im java man java java ice cream
Comments
Post a Comment