Skip to content

Commit 41f13f6

Browse files
committed
Update solutions
1 parent b18c380 commit 41f13f6

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* (Count vowels and consonants) Assume that the letters A, E, I, O, and U are vowels.
3+
* Write a program that prompts the user to enter a string, and displays the number of
4+
* vowels and consonants in the string.
5+
*
6+
* Enter a string: Programming is fun
7+
* The number of vowels is 5
8+
* The number of consonants is 11
9+
*
10+
* Created by Sven on 06/10/19.
11+
*/
12+
package Chapter05;
13+
14+
import java.util.Scanner;
15+
16+
public class Exercise0549_Count_vowels_and_consonants {
17+
public static void main(String[] args) {
18+
Scanner input = new Scanner(System.in);
19+
System.out.print("Enter a string: ");
20+
String str = input.nextLine();
21+
22+
int numOfVow = 0, numOfCon = 0;
23+
24+
for (int i = 0; i < str.length(); i++) {
25+
char ch = str.charAt(i);
26+
27+
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
28+
ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
29+
numOfVow++;
30+
} else if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
31+
numOfCon++;
32+
}
33+
}
34+
System.out.println("The number of vowels is " + numOfVow);
35+
System.out.println("The number of consonants is " + numOfCon);
36+
}
37+
}
38+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* (Print multiplication table) Write a program that uses for, while, and do-while loop
3+
* statements to display the multiplication table. Here is a sample run:
4+
*
5+
* Enter a string: Welcome to Java
6+
* 1*1= 1 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9
7+
* 1*2= 1 2*2= 2 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18
8+
* 1*3= 3 2*3= 6 3*3= 9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27
9+
* 1*4= 4 2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36
10+
* 1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45
11+
* 1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54
12+
* 1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63
13+
* 1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72
14+
* 1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
15+
*
16+
* Created by Sven on 06/10/19.
17+
*/
18+
package Chapter05;
19+
20+
import java.util.Scanner;
21+
22+
public class Exercise0550_Print_multiplication_table {
23+
public static void main(String[] args) {
24+
Scanner input = new Scanner(System.in);
25+
System.out.print("Enter a string: ");
26+
String str = input.nextLine();
27+
28+
// for loop
29+
System.out.println("\nPrint multiplication table using for loop");
30+
for (int i = 1; i < 10; i++) {
31+
for (int j = 1; j < 10; j++) {
32+
System.out.printf("%d*%d=%2d ", i, j, i * j);
33+
}
34+
System.out.println();
35+
}
36+
37+
// do-while loop
38+
System.out.println("\nPrint multiplication table using while loop");
39+
int i = 1, j = 1;
40+
while (i < 10) {
41+
while (j < 10) {
42+
System.out.printf("%d*%d=%2d ", i, j, i * j);
43+
j++;
44+
}
45+
System.out.println();
46+
i++;
47+
j = 1;
48+
}
49+
50+
51+
// do-while loop
52+
System.out.println("\nPrint multiplication table using do-while loop");
53+
i = 1;
54+
j = 1;
55+
do {
56+
do {
57+
System.out.printf("%d*%d=%2d ", i, j, i * j);
58+
j++;
59+
} while (j < 10);
60+
System.out.println();
61+
i++;
62+
j = 1;
63+
} while (i < 10);
64+
}
65+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* (Longest common prefix) Write a program that prompts the user to enter two
3+
* strings and displays the largest common prefix of the two strings. Here are some
4+
* sample runs:
5+
*
6+
* Enter the first string: Welcome to C++
7+
* Enter the second string: Welcome to programming
8+
* The common prefix is Welcome to
9+
*
10+
* Enter the first string: Atlanta
11+
* Enter the second string: Macon
12+
* Atlanta and Macon have no common prefix
13+
*
14+
* Created by Sven on 06/10/19.
15+
*/
16+
package Chapter05;
17+
18+
import java.util.Scanner;
19+
20+
public class Exercise0551_Longest_common_prefix {
21+
public static void main(String[] args) {
22+
Scanner input = new Scanner(System.in);
23+
24+
System.out.print("Enter the first string: ");
25+
String firstStr = input.nextLine();
26+
27+
System.out.print("Enter the second string: ");
28+
String secondStr = input.nextLine();
29+
30+
String commonPrefix = "";
31+
32+
int minLen = (firstStr.length() <= secondStr.length()) ? firstStr.length() : secondStr.length();
33+
for (int i = 0; i < minLen; i++) {
34+
if (firstStr.charAt(i) == secondStr.charAt(i)) {
35+
commonPrefix += firstStr.charAt(i);
36+
} else {
37+
break;
38+
}
39+
}
40+
41+
if (commonPrefix.length() == 0) {
42+
System.out.println(firstStr + " and " + secondStr + " have no common prefix");
43+
} else {
44+
System.out.println("The common prefix is " + commonPrefix);
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)