Programs on strings
Programs on strings
11. Write a program to input a string and print out the text with the uppercase
and lowercase letters reversed, but all other characters should remain the same as
before.
Example:
INPUT : WelComE TO School
OUTPUT : wELcOMe to SCHOOL
12. Write a program to find the number of vowels, consonants, digits and white
space (Character.isWhitespace(char)/relational operator) characters in a string.
*Character.isLetterOrDigit(char) – returns true if the character is a letter or a
digit.
13.
14. Write a program to input a string in uppercase and print the frequency of
each character.
Example:
15. Write a program to accept a string. Convert the string to uppercase. Count
and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4
16. Write a Java program to compare two strings lexicographically Two strings
are lexicographically equal if they are the same length and contain the same
characters in the same positions
17. Using the switch-case statement, write a menu driven program to do the
following:
(a) To generate and print Letters from A to Z and their Unicode Letters Unicode
21. Write a program that takes your full name as input and displays the
abbreviations of the first and middle names except the last name which is
displayed as it is. For example, if your name is Robert Brett Roser, then the output
should be R.B.Roser.
22. Write a program to check if the word 'orange' is present in the string "This is
orange juice".
23. Write a program in Java to accept a string in lower case and change the first
letter of every word to upper case. Display the new string.
Sample input: we are in cyber world
Sample output : We Are In Cyber World
24. Write a program to delete all consonants from the string "Hello, have a good
day".
25. Write a program to accept a word and convert it into lowercase if it is in
uppercase, and display the new word by replacing only the vowels with the
character following it:
Example:
Sample Input: computer
Sample Output: cpmpvtfr
26. Input a string of alphabets. Find out the number of occurrences of all
alphabets in that string. Find out the alphabet with maximum occurrence.
27. Write a program using a method isPalin(), to check whether a string is a
Palindrome or not. A Palindrome is a string that reads the same from left to right
and vice versa. E.g., MADAM, ARORA, ABBA, etc.
28. Write a Java program to return the sum of the digits present in the given
string. If there are no digits the sum returned is 0.
29. Design a class to overload a function compared as follows :
(a) void compare(int, int) — to compare two integer values and print the greater of
the two integers.
(b) void compare(char, char) — to compare the numeric value of two characters
and print the character with higher numeric value.
(c) void compare(String, String) — to compare the length of the two strings and
print the longer of the two.
30. Write a program that encodes a word into Piglatin. To translate word into a
Piglatin word, convert the word into uppercase and then place the first vowel of the
original word as the start of the new word along with the remaining alphabets. The
alphabets present before the vowel being shifted towards the end followed by
“AY”.
Sample input (1) : London, Sample output (1) : ONDONLAY
Sample input (2) : Olympics, Sample output (2) : OLYMPICSAY
31. Write a program to assign a full path and file name as given below. Using
library functions, extract and output the file path, file name and file extension
separately as shown.
Input C: / Users / admin / Pictures / flower.jpg
Output path: C: / Users/admin/Pictures/
File name: flower
Extension: jpg
32. Write a program to find out the largest and smallest word in the string "This
is an umbrella".
33. Write down the names of 10 of your friends in an array and then sort those in
alphabetically ascending order.
34. Define a class and store the given city names in a single dimensional array.
Sort these names in alphabetical order using the Bubble Sort technique only.
INPUT : Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT : Agra, Bangalore, Calcutta, Delhi, Mumbai
35. Input a string which contains some palindrome substrings. Find out the
position of palindrome substrings if exist and replace it by *. (For example, if input
string is “bob has a radar plane” then it should convert in “*** has a ***** plane”).
36. Write a program to replace a given substring in a sentence with another
string. For example, in the sentence, “A batman with bat” if we replace ”bat” with
”snow”, the new sentence should be printed as ”A snowman with snow”.
37. Write a program to reverse individual words in a string, where each word
may be delimited by a dot, comma, space or tab, like www.google.com should
become www.elgoog.moc.
38. Write a program to input a sentence and print the number of characters
found in the longest word of the given sentence.
For example, if S=“India is my country” then the output should be 7.
39. Design a class to overload a function num_calc() as follows :
(a) void num_calc (int num, char ch) with one integer argument and one character
argument, computes the square of integer argument if choice ch is ‘s’ otherwise
finds its cube.
(b) void num_calc (int a, int b, char ch) with two integer arguments and one
character argument. It computes the product of integer arguments if ch is ‘p’
else adds the integers.
(c) void num_calc (String s1, String s2) with two string arguments, which prints
whether the strings are equal or not.
40. Write a program to accept the names of 10 cities in a single dimension string
array and their STD (Subscribers Trunk Dialing) codes in another single
dimension integer array. Search for a name of a city input by the user in the list. If
found, display* “Search successful” and print the name of the city along with its
STD code, or else display the message “Search Unsuccessful, No such city in the
list”.
41.
Write a program to read the data, calculate and display the following :
(a) Average marks obtained by each student.
(b) Print the roll number and average marks of the students whose average mark is above 80.
(c) Print the roll number and average marks of the students whose average mark is below 40.
2. Write a program to store 6 elements in any array P, and 4 elements in an array Q and produce a third array R,
containing all the elements of array P and Q. Display the resultant array. [15]
3. Write a program to accept the year of graduation from school as an integer value from, the user. Using the
Binary Search technique on the sorted array of integers given below.
Output the message “Record exists” If the value input is located in the array. If not, output the message “Record
does not exist”.
{1982, 1987, 1993, 1996. 1999, 2003, 2006, 2007, 2009, 20101.
• SELINA SOLUTIONS
• ML AGGARWAL SOLUTIONS
• ISC & ICSE PAPERS
• MATHS
A Plus Topper
(Two Hours)
Answers to this Paper must be written on the paper provided separately.
You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the question paper.
The time given at the head of this Paper is the time allowed for writing the answers.
This paper is divided into two Sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in brackets [ ].
Question 1:
(a) What are the default values of the primitive data type int and float ? [2]
(b) Name any two OOP’s principles. [2]
(c) What are identifiers ? [2]
(d) Identify the literals listed below: [2]
(i) 0.5 (ii) ‘A’ (iii) false (iv) “a”
(e) Name the wrapper classes of char type and boolean type. [2]
Answer:
(a) Default values of:
int 0
float 0.0
Question 2:
(a) Evaluate the value of a. if value of p = 5, q = 19 [2]
int n = (q – p) > (p – q) ? (q – p) : (p – q);
(b) Arrange the following primitive-data types in an ascending order of their size:
(i) char (ii) byte (iii) double (iv) int [2]
(e) What are the values of a and b after the following function is executed, if the values passed are 30
and 50: [2]
void paws(int a, int b)
{
a=a + b;
b=a – b;
a=a – b;
System.out.println (a+ “,” +b);
}
Answer:
(a) 14.
(c) res =8
Question 3:
(a) State the data type and value of y after the following is executed:
char x=7;
y-Character.isLetter(x); [2]
(b) What is the function of catch block in exception handling ? Where does it appear in a program ? [2]
(c) State the output when the following program segment is executed:
String a =“Smartphone”, b=“Graphic Art”;
String h=a.substring(2, 5);
String k=b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h)); [2]
(d) The access specifier that gives the most accessibility is ………… and the least accessibility is …………
. [2]
(e) (i) Name the mathematical function which is used to find sine of an angle given in radians.
(ii) Name a string function which removes the blank spaces provided in the prefix and suffix of a
string. [2]
(c) Output:
art
true.
(d) The access specifier that gives the most accessibility is public and the least accessibility is private.
(h) output: a b c d
Question 4:
Define a class named ParkingLot with the following description :
Instance variables/data members:
int vno — To store the vehicle number
int hours — To store the number of hours the vehicle is parked in the parking lot
double bill — Tb store the bill amount
Member methods:
void input( ) — To input and store the vno and hours.
void calculate( ) — To compute the parking charge at the rate of Rs.3 for the first hours or part thereof,
and Rs. 1.50 for each additional hour or part thereof.
void display ( ) — To display the detail
Write a main method to create an object of the class and call the above methods. [15]
Answer:
Question 5:
Answer:
Question 6:
Write a program to input and store roll numbers, names and marks in 3 subjects of n number students
in five single dimensional array and display the remark based on average marks as given below : (The
maximum marks in the subject are 100) [15]
Answer:
Question 7:
Design a class to overload a function Joystring( ) as follows :
(i) void Joystring (String s, char ch1 char ch2) with one string argument and two character arguments
that replaces the character argument ch1 with the character argument ch2 in the given string s and
prints the new string.
Example:
Input value of s = “TECHNALAGY”
ch1=‘A’,
ch2=‘O’
Output: TECHNOLOGY
(ii) void Joystring (String s) with one string argument that prints the position of the first space and the
last space of the given string s.
Example:
Input value of = “Cloud computing means Internet based computing”
Output: First index : 5
Last index : 36
(iii) void Joystring (String s1, String s2) with two string arguments that combines the two string with a
space between them and prints the resultant string. Example :
Input value of s1 =“COMMON WEALTH”
Input value of s2 =“GAMES”
Output: COMMON WEALTH GAMES
(use library functions)
Question 6:
Special words are those words which starts and ends with the same letter. [15]
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and vice versa
Examples:
MALAYALAM
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 print Whether the word is a palindrome or only special word.
Write a program to initialize the seven Wonders of the World along with their locations in two different arrays.
Search for a name of the country input by the user. If found, display the name of the country along with its
Wonder, otherwise display “Sorry Not Found!”. [15]
Seven wonders — CHICHEN ITZA, CHRIST THE REDEEMER, TAJMAHAL, GREAT WALL OF CHINA, MACHU
PICCHU, PETRA, COLOSSEUM
Locations — MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example — Country Name: INDIA Output: INDIA-TAJMAHAL
Country Name: USA Output: Sorry Not Found!
(ii) void check(String si) – to display only vowels from string si, after converting it to lower case.
Example:
Input:
s1 = “computer”
Output:
oue
Question 9.
Write a program to input forty words in an array. Arrange these words in descending order of alphabets, using
selection sort technique. Print the sorted array.
Question 9.
Write a program to accept name and total marks of N number of students in two single subscript array name[]
and totalmarks[ ]. [15]
Calculate and print:
(i) The average of the total marks obtained by N Number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student’s total marks with the average.
[deviation = total marks of a student – average] ‘