Skip to content

Commit 2fba492

Browse files
authored
Add files via upload
1 parent bf2aa9d commit 2fba492

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

14.04_ComparePortions/Main.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
3+
class Main {
4+
public static void main(String[] args) {
5+
// Get 2 strings from user
6+
Scanner input = new Scanner(System.in);
7+
System.out.print("Enter first string: ");
8+
String string1 = input.next();
9+
System.out.print("Enter second string: ");
10+
String string2 = input.next();
11+
12+
// Number of characters to compare
13+
int numOfCharacters = 0;
14+
if (string1.length()>string2.length())
15+
numOfCharacters = string1.length();
16+
else
17+
numOfCharacters = string2.length();
18+
19+
// Starting index of comparison
20+
int startIndex = 0;
21+
22+
// State whether strings are equal
23+
if (string1.regionMatches(true, startIndex, string2, startIndex, numOfCharacters))
24+
System.out.println("The strings match!");
25+
else
26+
System.out.println("The strings do not match.");
27+
28+
29+
}
30+
}

14.11_SearchStrings/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Main {
2+
public static void main(String[] args) {
3+
// input line of text
4+
String string1 = "greedy goats gobbled up gooseberries, getting good at grabbing the goodies";
5+
6+
// number of char occurances using indexOf
7+
int i = string1.indexOf('g');
8+
int numOfOccurences = 0;
9+
while (i != -1){
10+
numOfOccurences++;
11+
i = string1.indexOf('g',i+1);
12+
}
13+
14+
System.out.println("The letter 'g' occurs in the line ");
15+
System.out.println(string1);
16+
System.out.println(+numOfOccurences +" times.");
17+
18+
19+
}
20+
}

0 commit comments

Comments
 (0)