단어의 개수
문제
내 풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
str = str.trim();
int count = 0;
if(str.length() == 0)
{
System.out.print("0");
return;
}
for(int i = 0; i < str.length(); i++)
{
if(str.charAt(i) == ' ')
count++;
}
System.out.println(count + 1);
}
}
다른 풀이
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
int count = 0;
int pre_str = 32;
int str ;
while(true) {
str = System.in.read();
if(str == 32) {
if(pre_str != 32) count++;
}
else if(str == 10) {
if(pre_str != 32) count++;
break;
}
pre_str = str;
}
System.out.println(count);
}
}
출처
https://st-lab.tistory.com/65