0% found this document useful (0 votes)
2 views9 pages

String Handling - Example Programs

The document contains a series of Java programming exercises focused on string handling and array manipulation, suitable for Class X ICSE students. Each program includes specific tasks such as displaying letters of a word, counting vowels, replacing characters, checking for palindromes, and sorting names or words. Additionally, it features pattern generation and sorting techniques using bubble sort and selection sort.

Uploaded by

nanditha629
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

String Handling - Example Programs

The document contains a series of Java programming exercises focused on string handling and array manipulation, suitable for Class X ICSE students. Each program includes specific tasks such as displaying letters of a word, counting vowels, replacing characters, checking for palindromes, and sorting names or words. Additionally, it features pattern generation and sorting techniques using bubble sort and selection sort.

Uploaded by

nanditha629
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Class X ICSE – String handling example programs

The question given have programs in some cases and only the algorithm(logic part) in others. Please complete the
whole program while copying it down in your books.

Program 1
Write a program to display each letter of a word entered by the user.
Input: COMPUTER
Output:
C
O
M
P
U
T
E
R

String st = in.next();
int b=st.length();
for(int a=0; a<b; a++){
System.out.println(st.charAt(a));
}

Program 2:
Write a program to accept a String. Count and display the number of vowels present in the String

st = in.nextLine();
b = st.length();
char ch;
int v=0;
for(int a=0; a<b; a++){
ch = st.charAt(a)
if(ch==’a’ || ch==’e’ || ch==’i’ || ch==’o’ || ch==’u’ || ch==’A’ || ch==’E’ || ch==’I’ || ch==’O’ || ch==’U’){
v++;
}
}
System.out.println(v);

Program 3:
Write a program to accept a String in lowercase and replace ‘e’ with ‘*’ in the given string. Display the new string.

st = in.nextLine();
b = st.length();
char ch;
for(int a=0; a<b; a++){
ch = st.charAt(a)
if(ch==’e’){
ch=’*’;
}
System.out.println(ch)
}
Program 4:
Write a program to accept a string and change the case of each letter of the String. Display the new string.

String st, stNew=””;


st = in.nextLine();
b = st.length();
char ch;
for(int a=0; a<b; a++){
ch = st.charAt(a)
if(ch>=’a’ && ch<=’z’){
newCh = Character.toUppercase(ch);
stNew = stNew+newCh;
}
else if(ch>=’A’ && ch<=’Z’){
newCh = Character.toLowercase(ch);
stNew = stNew+newCh;
}
else{
stNew = stNew+newCh;
}
}
System.out.println(stNew)

Program 5:
Write a program in java to accept a word and check whether the word is Palindrome or not. [2022]

String word = sc.nextLine();


word = word.toLowerCase();

boolean isPalindrome = true;


int length = word.length();
for (int i = 0; i < length / 2; i++) {
if (word.charAt(i) != word.charAt(length - 1 - i)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println(word + " is a Palindrome.");
} else {
System.out.println(word + " is NOT a Palindrome.");
}
Program 6:
Write a program in java to accept a sentence in lowercase. Convert first letter of each word of the sentence in
uppercase. [2018]

System.out.print("Enter a sentence in lowercase: ");


String sentence = sc.nextLine().toLowerCase();
sentence=’ ‘+sentence;

String result = "";

// Iterate from index 1 onwards


for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);

if (sentence.charAt(i) == ' ') {


result = result+ ‘ ‘ +Character.toUpperCase(sentence.charAt(i+1));
} else {
result += ch;
}
}
System.out.print (result);

Program 7:
Write a program in java to accept a word and display the same in PigLatin form. [2013]
Sample Input: TROUBLE
Sample Output: OUBLETRAY

String word = sc.nextLine().toUpperCase();


String pigLatin = "";
int vowelIndex = -1;
int len = word.length();

// Find the index of the first vowel


for (int i = 0; i < len; i++) {
char ch = word.charAt(i);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
vowelIndex = i;
break;
}
}

// Construct PigLatin form


if (vowelIndex == 0) {
pigLatin = word + "AY";
} else if (vowelIndex > 0) {
String prefix = word.substring(0, vowelIndex);
String suffix = word.substring(vowelIndex);
pigLatin = suffix + prefix + "AY";
}

System.out.println("PigLatin form: " + pigLatin);


Program 8:
Define a class to declare a character array of size 10. Accept the characters into the array and display the
characters with the highest and lowest ASCII values. [2022]
Sample input: {‘R’, ‘z’, ‘q’, ‘A’, ‘N’, ‘p’, ‘m’, ‘U’ , ‘Q’, ‘F’}
Sample output:
Character with highest ASCII value: z
Character with lowest ASCII value = A

char[] ch = new char[10];


System.out.println("Enter 10 characters:");
for (int i = 0; i < 10; i++) {
ch[i] = sc.next().charAt(0);
}

char highest = ch[0];


char lowest = ch[0];

for (int i = 1; i < 10; i++) {


if (ch[i] > highest) {
highest = ch[i];
}
if (ch[i] < lowest) {
lowest = ch[i];
}
}

System.out.println("Character with highest ASCII value: " + highest);


System.out.println("Character with lowest ASCII value: " + lowest);

Prog 18. [2018]


Write a menu driven program to display the pattern as per the user’s choice.
Pattern 1:
ABCDE
ABCD
ABC
AB
A

Pattern 2:
B
LL
UUU
EEEE
J J J J J
import java.util.Scanner;

public class PatternMenu {

// Method to display Pattern 1


static void displayPattern1() {
for (int i = 5; i >= 1; i--) {
char ch = 'A';
for (int j = 1; j <= i; j++) {
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
}

// Method to display Pattern 2


static void displayPattern2() {
char[] letters = {'B', 'L', 'U', 'E', 'J'};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(letters[i] + " ");
}
System.out.println();
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
int choice;

System.out.println("\n--- Pattern Menu ---");


System.out.println("1. Display Pattern 1");
System.out.println("2. Display Pattern 2");
System.out.print("Enter your choice (1-2): ");

choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("\nPattern 1:");
displayPattern1();
break;
case 2:
System.out.println("\nPattern 2:");
displayPattern2();
break;
default:
System.out.println("Invalid choice. Please enter 1 or 2.");
}
}
}
Prog 21. [2023]
Define a class to accept 10 characters( lowercase/uppercase) from the user in an array. Using bubblesort
technique, arrange them in ascending order.

import java.util.Scanner;

public class CharBubbleSort {


// Main method
public static void main(String[] args) {
char[] characters = new char[10];
Scanner scanner = new Scanner(System.in);

// Accept 10 characters from the user


System.out.println("Enter 10 characters (uppercase/lowercase):");
for (int i = 0; i < 10; i++) {
System.out.print("Character " + (i + 1) + ": ");
characters[i] = scanner.next().charAt(0);
}

// Bubble sort to arrange characters in ascending order


for (int i = 0; i < characters.length - 1; i++) {
for (int j = 0; j < characters.length - 1 - i; j++) {
if (characters[j] > characters[j + 1]) {
// Swap
char temp = characters[j];
characters[j] = characters[j + 1];
characters[j + 1] = temp;
}
}
}

// Display sorted characters


System.out.println("\nCharacters in ascending order:");
for (char ch : characters) {
System.out.print(ch + " ");
}
}
}
Prog 22. [2022]
Define a class to declare an array to accept and store 10 names/words. Display only those names/words which
begin with the letter ‘A’ or ‘a’ and end with the letter ‘A’ or a ‘a’
import java.util.Scanner;

public class Words {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] names = new String[10];

// Accept 10 names/words
System.out.println("Enter 10 names or words:");
for (int i = 0; i < 10; i++) {
names[i] = scanner.nextLine();
}

// Display names that start and end with 'A' or 'a'


System.out.println("\nWords that start and end with 'A' or 'a':");
for (int i = 0; i < 10; i++) {
String word = names[i];
if (word.length() > 0) {
char firstChar = word.charAt(0);
char lastChar = word.charAt(word.length() - 1);

if ((firstChar == 'A' || firstChar == 'a') &&(lastChar == 'A' || lastChar == 'a')) {


System.out.println(word);
}
}
}
}
}
Prog 24. [2008, 2015]
Write a program to accept 10 different city names in a single dimensional array. Arrange the names in ascending
order by using the bubble sort technique and display them.

import java.util.Scanner;

public class CitySorter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] cities = new String[10];

// Accept 10 city names


System.out.println("Enter 10 different city names:");
for (int i = 0; i < 10; i++) {
System.out.print("City " + (i + 1) + ": ");
cities[i] = scanner.nextLine();
}

// Bubble Sort to arrange cities in ascending (alphabetical) order


for (int i = 0; i < cities.length - 1; i++) {
for (int j = 0; j < cities.length - 1 - i; j++) {
if (cities[j].compareToIgnoreCase(cities[j + 1]) > 0) {
String temp = cities[j];
cities[j] = cities[j + 1];
cities[j + 1] = temp;
}
}
}

// Display sorted cities


System.out.println("\nCities in ascending order:");
for (int i = 0; i < cities.length; i++){
System.out.println(cities[i]);
}
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}

}
}
Prog 25. [2017]
Write a program to input forty words in an array. Arrange these words in descending order of letters by using
Selection sort technique. Print the sorted array.

import java.util.Scanner;

public class DescendingWordSorter {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] words = new String[40];

// Input 40 words from the user


System.out.println("Enter 40 words:");
for (int i = 0; i < 40; i++) {
System.out.print("Word " + (i + 1) + ": ");
words[i] = scanner.nextLine();
}

// Selection sort in descending (Z to A) order


for (int i = 0; i < words.length - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < words.length; j++) {
if (words[j].compareToIgnoreCase(words[maxIndex]) > 0) {
maxIndex = j;
}
}

// Swap if a larger word is found


if (maxIndex != i) {
String temp = words[i];
words[i] = words[maxIndex];
words[maxIndex] = temp;
}
}

// Display the sorted array


System.out.println("\nWords in descending order:");
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
}
}

You might also like