-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea019a2
commit 7835728
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
``` |