Skip to content

Commit

Permalink
Create 특정문자뒤집기.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ultramancode committed Apr 4, 2023
1 parent ea019a2 commit 7835728
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions 특정문자뒤집기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
**특정 문자 뒤집기**

설명

영어 알파벳과 특수문자로 구성된 문자열이 주어지면 영어 알파벳만 뒤집고,

특수문자는 자기 자리에 그대로 있는 문자열을 만들어 출력하는 프로그램을 작성하세요.

입력

첫 줄에 길이가 100을 넘지 않는 문자열이 주어집니다.

출력

첫 줄에 알파벳만 뒤집힌 문자열을 출력합니다.

예시 입력 1

```
a#b!GE*T@S
```

예시 출력 1

```
S#T!EG*b@a
```

lt =left // rt = right

while문 안에

if (lt == 특수문자) 그냥 lt++ (한칸 스킵이나 마찬가지)

else if (rt == 특수문자) rt - - (아 오른쪽에서 시작이니 -가 한칸스킵이구나)

else 교환 작업 시작

---

`if(Character.*isAlphabetic*(s[lt]))` → 알파벳이면 참

Character의 isAlphabetic() 기능

```java
import java.util.Scanner;

class Main {
public String solution(String str) {
String answer;
char[] s = str.toCharArray();
int lt = 0, rt = str.length() - 1;
while (lt < rt) {
// 정중앙 바뀌면 rt>lt되니 반복문 종료됨
if (!Character.isAlphabetic(s[lt])) lt++;
//알파벳이 아니면 lt 증가!! !Character.isAlphabetic()이니까
//한칸 스킵하고 다음 문자 확인
else if (!Character.isAlphabetic(s[rt])) rt--;
//알파벳 아니면 rt 감소! 한칸 스킵하고 다음 문자 확인
//둘 다 아니면 알파벳이라는 뜻이니 조건식 바로
else {
char tmp = s[lt];
s[lt] = s[rt];
s[rt] = tmp;
lt++;
rt--;
}
}
answer = String.valueOf(s);
// static 메소드인 .valueOf로 String 화 시켜주기
return answer;
}

public static void main(String[] args) {
Main T = new Main();
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(T.solution(str));
}
}
```

0 comments on commit 7835728

Please sign in to comment.