tp[
tp[
on Strings
Question 1
/*
* Number of words in a sentence are one
more than
* the number of spaces so incrementing
wCount by 1
*/
wCount++;
Output
Question 2
char ch =
Character.toUpperCase(str.charAt(i));
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
continue;
}
Output
Question 3
Output
Question 4
/*
* Get the last index
* of space in the string
*/
int lastSpaceIdx = name.lastIndexOf(' ');
String surname =
name.substring(lastSpaceIdx + 1);
String initialName = name.substring(0,
lastSpaceIdx);
Output
Question 5
if (word.length() >
lWord.length())
lWord = word;
word = "";
}
else {
word += ch;
}
}
Output
Question 6
Output
Question 7
System.out.println(newStr);
}
}
Output
Question 8
System.out.println(word);
}
}
Output
Question 9
if (isPalin)
System.out.println(word);
word = "";
}
else {
word += ch;
}
}
}
}
Output
Question 10
Output
Question 11
char ch =
Character.toUpperCase(str.charAt(i));
if (ch == 'A' ||
ch == 'E' ||
ch == 'I' ||
ch == 'O' ||
ch == 'U') {
count++;
}
Output
Question 12
System.out.println(newStr);
}
}
Output
Question 13
}
else {
newStr = newStr + ch;
}
}
System.out.println(newStr);
}
}
Output
Question 14
System.out.println(newStr);
}
}
Output
Question 15
long wordValue =
Long.parseLong(wordValueStr);
boolean isHappy =
isHappyNumber(wordValue);
if (isHappy)
System.out.println("A Happy Word");
else
System.out.println("Not a Happy
Word");
}
}
Output
Question 16
System.out.println("Character\tFrequency");
for (int i = 0; i < freqMap.length; i++)
{
if (freqMap[i] > 0) {
System.out.println((char)(i + 65)
+ "\t\t" +
freqMap[i]);
}
}
}
}
Output
Question 17
System.out.println("Double Letter
Sequence Count = " + count);
}
}
Output
Question 18
Special words are those words which start and end with
the same letter.
Example: EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same
from left to right and vice-versa.
Example: MALYALAM, MADAM, LEVEL, ROTATOR,
CIVIC
All palindromes are special words but all special words
are not palindromes.
Write a program to accept a word. Check and display
whether the word is a palindrome or only a special word
or none of them.
import java.util.Scanner;
if (isPalin) {
System.out.println("Palindrome");
}
else {
System.out.println("Special");
}
}
else {
System.out.println("Neither Special
nor Palindrome");
}
}
}
Output
Question 19
word = "";
}
else {
word += str.charAt(i);
}
}
System.out.println("Number of words
containing consecutive letters: " + count);
}
}
Output
Question 20
(a)
BLUEJ
BLUE
BLU
BL
B
import java.util.Scanner;
Output
(b)
J
EE
UUU
LLLL
BBBBB
import java.util.Scanner;
Output
(c)
BLUEJ
LUEJ
UEJ
EJ
J
import java.util.Scanner;
Output
Question 21
Write a program to display the pattern:
(a)
ABCDE
BCDE
CDE
DE
E
public class KboatStringPattern
{
public static void main(String args[]) {
String word = "ABCDE";
int len = word.length();
Output
(b)
A
BC
DEF
GHIJ
KLMNO
public class KboatStringPattern
{
public static void main(String args[]) {
char ch = 'A';
(c)
ABCDE
ABCDA
ABCAB
ABABC
AA B C D
public class KboatStringPattern
{
public static void main(String args[]) {
String word = "ABCDE";
int len = word.length();
Output
Question 22
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:
* * * * *
* * * *
* * *
* *
*
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 5
Sample Output:
A B C D E
A B C D
A B C
A B
A
import java.util.Scanner;
switch (choice) {
case 1:
for (int i = 0; i <= n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(' ');
}
for (int k = i; k <= n; k++)
{
System.out.print('*');
}
System.out.println();
}
break;
case 2:
n += 64;
for (int i = n; i >= 65; i--) {
for (int j = 65; j <= i; j++)
{
System.out.print((char)j);
}
System.out.println();
}
break;
default:
System.out.println("Incorrect
choice");
break;
}
}
}
Output
Question 23
Write a program to generate a triangle or an inverted
triangle based upon User’s choice.
Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter a word : BLUEJ
Sample Output:
B
LL
UUU
EEEE
JJJJJ
Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter a word : BLUEJ
Sample Output:
BLUEJ
BLUE
BLU
BL
B
import java.util.Scanner;
switch (choice) {
case 1:
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
System.out.print(word.charAt(i));
}
System.out.println();
}
break;
case 2:
for(int i = len - 1; i >= 0; i--)
{
for(int j = 0; j <= i; j++) {
System.out.print(word.charAt(j));
}
System.out.println();
}
break;
default:
System.out.println("Incorrect
choice");
break;
}
}
}
Output
Question 24
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j)
+ " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect
Choice");
}
}
}