Created
October 21, 2014 07:12
-
-
Save volgar1x/7043c18d2e16f6fdb767 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package shitty.iut; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Main { | |
public static List<String> getWords(String line) { | |
List<String> result = new ArrayList<String>(); | |
StringBuilder word = new StringBuilder(); | |
for (int i = 0; i < line.length(); i++) { | |
char c = line.charAt(i); | |
if (Character.isAlphabetic(c) || Character.isDigit(c)) { | |
word.append(c); | |
} else if (word.length() > 0) { | |
result.add(word.toString()); | |
word.setLength(0); | |
} | |
} | |
if (word.length() > 0) { | |
result.add(word.toString()); | |
} | |
return result; | |
} | |
public static String readLine() { | |
Scanner scanner = new Scanner(System.in); | |
return scanner.nextLine(); | |
} | |
public static void main(String[] args) { | |
// prompt(); | |
test("il y a des ponctuations!"); | |
test("j'ai des apostrophes"); | |
test("j'ai des accents éèù"); | |
test(""); | |
} | |
private static void test(String s) { | |
System.out.printf("%2$d words %1$s\n", s, getWords(s).size()); | |
} | |
private static void prompt() { | |
String line = readLine(); | |
List<String> words = getWords(line); | |
System.out.printf("il y a %d mots", words.size()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment