1) 문자찾기
설명
package com.company;
import java.util.Scanner;
public class MainCharSearch {
public void solution(String str, char c) {
int count = 0;
str.toUpperCase();
Character.toUpperCase(c);
for(char x : str.toCharArray()) {
if (x == c) {
count++;
}
}
System.out.println(count);
}
public static void main(String[] args) {
MainCharSearch T = new MainCharSearch();
Scanner in = new Scanner(System.in);
//입력
String str = in.next();
char c = in.next().charAt(0);
T.solution(str, c);
}
}
Java
복사
2) 대소문자 변환
설명
package com.company;
import java.util.Scanner;
public class MainUpperLower {
public static String solution(String str) {
String answer = "";
for (char c : str.toCharArray()) {
if (isUpper(c)) {
answer += Character.toLowerCase(c);
} else {
answer += Character.toUpperCase(c);
}
}
return answer;
}
//대문자 체크
public static boolean isUpper(char c) {
boolean result = true;
if (Character.isLowerCase(c)) {
result = false;
}
return result;
}
public static void main(String[] args) {
// 입력값
Scanner in = new Scanner(System.in);
String str = in.next();
//로직
System.out.println(solution(str));
}
}
Java
복사
3) 문장속 단어 찾기
설명
package com.company;
import java.util.Scanner;
public class searchChar {
public String solution(String str) {
String answer = "";
int min = Integer.MIN_VALUE;
String[] s = str.split(" ");
for (String s1 : s) {
int len = s1.length();
if (len > min) {
min = len;
answer = s1;
}
}
System.out.println(answer);
return answer;
}
public static void main(String[] args) {
searchChar searchChar = new searchChar();
//입력
Scanner in = new Scanner(System.in);
String str = in.nextLine();
//로직
searchChar.solution(str);
}
}
Java
복사
4) 단어 뒤집기
설명
package com.company;
import java.util.ArrayList;
import java.util.Scanner;
class MainRevers {
//단순한 리버스
// static public ArrayList<String> solution(String[] str) {
// ArrayList<String> strings = new ArrayList<>();
// for (String string : str) {
// String temp = new StringBuilder(string).reverse().toString();
// strings.add(temp);
// }
// return strings;
// }
// 지정 리버스
static public ArrayList<String> solution(String[] str) {
ArrayList<String> strings = new ArrayList<>();
for (String string : str) {
char[] chars = string.toCharArray(); // String을 캐릭터로 변경된다.
int lt = 0, rt= string.length()-1;
while(lt < rt) {
char tmp = chars[lt];
chars[lt] = chars[rt];
chars[rt] = tmp;
lt ++;
rt --;
}
String temp = string.valueOf(chars);
strings.add(temp);
}
return strings;
}
public static void main(String[] args) {
MainRevers T = new MainRevers();
Scanner in = new Scanner(System.in);
// 입력값
int inputNumber = in.nextInt();
String[] str = new String[inputNumber];
for (int i = 0; i < str.length; i++) {
str[i] = in.next();
}
// 결과 값
ArrayList<String> solution = solution(str);
for (String s : solution) {
System.out.println(s);
}
}
}
Java
복사
5) 특정 문자 뒤집기
설명
package com.company;
import java.util.Scanner;
public class SearchSpecialSentence {
public String solution(String str) {
String answer = "";
char[] s = str.toCharArray();
int lt = 0, rt = s.length -1;
while(lt<rt) {
if (!isAlpha(s[lt])) {
lt++;
} else if (!isAlpha(s[rt])) {
rt--;
} else {
char temp = s[lt];
s[lt] = s[rt];
s[rt] = temp;
lt++;
rt--;
}
}
answer = String.valueOf(s);
System.out.println(answer);
return answer;
}
private boolean isAlpha(char c) {
boolean result = true;
if (!Character.isAlphabetic(c)) {
result = false;
}
return result;
}
public static void main(String[] args) {
SearchSpecialSentence searchChar = new SearchSpecialSentence();
Scanner in = new Scanner(System.in);
String str = in.nextLine();
searchChar.solution(str);
}
}
Java
복사
6) 중복문자 제거
package com.company;
import java.util.Scanner;
public class RemoveDuplicateChar {
public String solution(String str) {
String answer = "";
for (int i = 0; i < str.length(); i++) {
// System.out.println(str.charAt(i) + "/" + str.indexOf(str.charAt(i)) +"/"+ i);
if (isSameNumber(str.indexOf(str.charAt(i)), i)) {
answer += str.charAt(i);
}
}
System.out.println(answer);
return answer;
}
private boolean isSameNumber(int n1, int n2) {
boolean result = true;
if (n1 != n2) {
result = false;
}
return result;
}
public static void main(String[] args) {
RemoveDuplicateChar searchChar = new RemoveDuplicateChar();
Scanner in = new Scanner(System.in);
String str = in.nextLine();
searchChar.solution(str);
}
}
Java
복사
7) 회문문자열
설명
package com.company;
import java.util.Scanner;
public class SearchPalindrome {
public String solution(String str) {
str = str.toLowerCase();
String answer = "NO";
StringBuilder stringBuilder = new StringBuilder(str);
String reversStr = stringBuilder.reverse().toString();
if(str.equals(reversStr)) {
answer = "YES";
}
System.out.println(answer);
return answer;
}
public static void main(String[] args) {
SearchPalindrome searchChar = new SearchPalindrome();
Scanner in = new Scanner(System.in);
String str = in.nextLine();
searchChar.solution(str);
}
}
Java
복사
8) 팰린드롬 정규식 특수문자 제거
설명
package com.company;
import java.util.Scanner;
public class SearchPalindromeEffective {
public String solution(String str) {
String answer = "NO";
// 특수문자 삭제
str = str.toUpperCase().replaceAll("[^A-Z]", "");
String reversStr = new StringBuilder(str).reverse().toString();
if (str.equals(reversStr)) {
answer = "YES";
}
System.out.println(answer);
return answer;
}
public static void main(String[] args) {
SearchPalindromeEffective searchChar = new SearchPalindromeEffective();
Scanner in = new Scanner(System.in);
String str = in.nextLine();
searchChar.solution(str);
}
}
Java
복사
9) 숫자만 추출
설명
package com.company;
import java.util.Scanner;
public class ExtraNumber {
public int solution(String str) {
int answer = 0;
char [] chars = str.toCharArray();
for (char c : chars) {
if (c >= 48 && c <= 57) {
answer = answer*10+(c-48);
}
}
return answer;
}
public static void main(String[] args) {
ExtraNumber extraNumber = new ExtraNumber();
Scanner in = new Scanner(System.in);
String str = in.next();
System.out.println(extraNumber.solution(str));
}
}
Java
복사
10) 가장 짧은 문자거리
설명
package com.company;
import java.util.Scanner;
public class DistanceChar {
public void solution(String str, char c) {
int[] answer = new int[str.length()];
char[] chars = str.toCharArray();
int p = 1000;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == c) {
p = 0;
} else {
p++;
}
answer[i] = p;
}
p = 1000;
for (int i = chars.length-1; i >= 0; i--) {
if (chars[i] == c) {
p = 0;
} else {
p++;
answer[i] = Math.min(answer[i], p);
}
}
for (int i : answer) {
System.out.print(i);
}
}
public static void main(String[] args) {
DistanceChar distanceChar = new DistanceChar();
Scanner in = new Scanner(System.in);
String str = in.next();
char c = in.next().charAt(0);
distanceChar.solution(str, c);
}
}
Java
복사
11) 문자열 압축 문제
설명
package com.company;
import java.util.Scanner;
public class CompressionString {
public String solution(String str) {
String answer = "";
str += " ";
char [] chars = str.toCharArray();
int cnt = 1;
for (int i = 0; i < str.length()-1; i++) {
if (chars[i] == chars[i+1]) {
cnt += 1;
} else {
answer += chars[i];
if (cnt > 1) {
answer += cnt;
}
cnt = 1;
}
}
System.out.println(answer);
return answer;
}
public static void main(String[] args) {
CompressionString compressionString = new CompressionString();
Scanner in = new Scanner(System.in);
String str = in.nextLine();
compressionString.solution(str);
}
}
Java
복사
12) 암호(replace(), parseInt(String, 2))
설명
package com.company;
import java.util.Scanner;
public class SecretLetter {
public String solution(int number, String str) {
String answer = "";
for (int i = 0; i < number; i++) {
String temp = str.substring(0, 7);
str = str.substring(7);
int n = convertStringToNumber(temp);
answer += (char)n;
}
return answer;
}
public int convertStringToNumber(String temp) {
String str = temp.replace("#", "1").replace("*", "0");
return Integer.parseInt(str, 2);
}
public static void main(String[] args) {
SecretLetter secretLetter = new SecretLetter();
Scanner in = new Scanner(System.in);
int number = in.nextInt();
String str = in.next();
System.out.println(secretLetter.solution(number, str));
}
}
Java
복사