forked from angiejones/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextProcessor.java
More file actions
57 lines (45 loc) · 1.43 KB
/
Copy pathTextProcessor.java
File metadata and controls
57 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package chapter8;
public class TextProcessor {
public static void main(String[] args){
countWords("I love Test Automation University");
reverseString("Hello TAU!");
addSpaces("HeyWorld!It'sMeAngie");
}
/**
* Splits a String into an array by tokenizing it.
* Counts words and prints them
* @param text Full string to be split
*/
public static void countWords(String text){
var words = text.split(" ");
int numberOfWords = words.length;
String message = String.format("Your text contains %d words:", numberOfWords);
System.out.println(message);
for(int i=0; i<numberOfWords; i++){
System.out.println(words[i]);
}
}
/**
* Prints a String in reverse order
* @param text String to reverse
*/
public static void reverseString(String text){
for(int i=text.length()-1; i>=0; i--){
System.out.print(text.charAt(i));
}
}
/**
* Adds spaces before each uppercase letter
* @param text jumbled text
*/
public static void addSpaces(String text){
var modifiedText = new StringBuilder(text);
for(int i=0; i< modifiedText.length(); i++){
if(i!=0 && Character.isUpperCase(modifiedText.charAt(i))){
modifiedText.insert(i, " ");
i++;
}
}
System.out.println(modifiedText);
}
}