문제

I have problem with this task:

User will type some text.

  1. If text has 'a' triple this letter.
  2. If text has simply 'd' delete this letter.
  3. If text has double 'b' write only one 'b'.

I understand this outlines but I do not know how to do it. How should I search text with charAt? What if I find where it is, where should I write this trippled letter?

도움이 되었습니까?

해결책

It really is not that difficult! Go about it sequentially. First replace all the "a"s as you want them to be replaced, then all the "d"s and then all the "b"s. Here's a simple example with replace():

public static void main(String args[]) {
    System.out.print("Word:");
    Scanner scanner = new Scanner(System.in);
    String foo = scanner.next();
    foo = foo.replace("a", "aaa");
    System.out.println(foo);
    foo = foo.replace("d", "");
    System.out.println(foo);
    foo = foo.replace("bb", "b");
    System.out.println(foo);
}

Let me know if this is what you wanted. Also, you could do this with charAt() and String manipulation as well, but that would be a bit more involved.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top