Strong
Strong
CLASS XI
StringBuffer Class: - Using StringBuffer class, you can create modifiable string.
Declaration of StringBuffer object: StringBuffer s=new StringBuffer();
Initialisation of StringBuffer object: StringBuffer str=new StringBuffer(“India”);
Methods of StringBuffer class: -
1. length(): return the length of the string.
2. charAt(): return a single character from the string at particular index number.
Example: StringBuffer s=new StringBuffer(“Computer”);
System.out.println(s.length());
System.out.println(s.chaAt(5));
Output: 8
t
3. reverse(): It reverse the content of StringBuffer.
Example: StringBuffer s=new StringBuffer(“India”);
s.reverse();
System.out.println(s);
Output: aidnI
4. append(): StringBuffer class provides to overloaded append() method to allow
various data type values to be added to the end of the StringBuffer. Each of the
methods takes its argument converts it to a string and appends it to the
StringBuffer.
Example: String s=”Good Bye”;
booean b=true;
char ch=’z’;
int d=7;
StringBuffer str=new StringBuffer(s);
str.append(b);
str.append(“ “);
str.append(ch);
str.append(d);
System.out.println(str):
Output: Good Byetrue z7
Page | 1
UMESH COMPUTER KENDRA (PH: 9897188951)
5. insert(): This function takes two arguments one is index number and second is
value which you want to insert. StringBuffer class provided overloaded insert()
method to allow various datatype values to be inserted. Each of the methods take
its second argument, convert it to a string and insert it preceding the index
specified by the first argument.
Example: String s=”good”;
StringBuffer str=new StringBuffer(“morning”);
Str.insert(0,s);
Str.insert(5,true);
Str.insert(7,7);
System.out.println(str);
Output: goodmtr7ueorning
6. delete(): This function takes two argument, one is starting index number and
second is index one more than the end of character to be deleted. All characters
from the starting index number up to but not including the ending index number
will be deleted.
Example: StringBuffer str=new StringBuffer(“India is Great”);
str.delete(6,10);
System.out.println(str);
Output: India reat
7. deleteCharAt(): This method takes one argument. i.e. the index number of the
character to be deleted.
Example: StringBuffer s=new StringBuffer(“INDIA”);
s.deleteCharAt(2);
System.out.println(s);
Output: INIA
8. setCharAt(): This method takes an integer and a character argument and sets the
character at the specified position. If index given is out of range then
StringIndexOutOfBound exception is generated.
Example: StringBuffer s=new StringBuffer(“HAPPY”);
s.setCharAt(4,’A’);
System.out.println(s);
Output: HAPPA
Page | 2
UMESH COMPUTER KENDRA (PH: 9897188951)
Question 1.
Write a program which accept a string and a word, print the frequency of inputted word
in the given string.
Example: Input: String: - the quick fox jumps over the lazy dog
Word: - the
Output: 2 times
Question 2.
Write a program to accept a String and print the largest word of the string with its length.
Example: Input: My name is Joy
Output: Largest word is : name
Length of word is : 4
Question 3.
Write a accept a string, count and print number of words. There may be may number of
blanks between two words. There may be leading and trailing blacks.
Example: Input: we are in cyber world
Output: Number of words : 5
Question 4.
Write a program which accept a string and delete all the vowels present in the string
and display the resultant string.
Example: Input: Computer Application
Output: Cmptr pplcton
Question 5.
Write a program to accept a string in lower case and change the first letter of each word
to upper case and display the new String.
Example: Input: we are in cyber world
Output: We Are In Cyber World
Page | 3
UMESH COMPUTER KENDRA (PH: 9897188951)
Question 2005.
A class stringop is designed to handle string related operations. Some members of the
class are given below:
Date member
txt : to store the given string of maximum length 100.
Member function
stringop() : constructor
void readstring() : to accept the string
char caseconvert(int, int): to convert the letter to other case
void circulardecode() : to decode the string by replacing each letter by converting it
to opposite case and then by the next character in a circular way. Hence “AbZ” will
decode “bCa”.
Specify the class giving details of all member functions . You do not need to write
function main().
Question 2006.
Design a class Stringfun to perform various operations on strings without using built –in
functions except for finding length of the string. Some of the member functions and data
members are given below:
Class name : Stringfun
Data members
str : to store a string
Member functions
void input() : to accept the string
void words() : to find and display the number of words, number of vowels and number
of uppercase characters in the string.
void frequency() : to display the frequency of each character within the string.
Specify the class Stringfun giving details of the functions void input(), void words() and
void frequency(). You do not need to write the main function.
Page | 4
UMESH COMPUTER KENDRA (PH: 9897188951)
Question 2007.
A class Mystring has been defined for the following methods:
Class name : Mystring
Data members
str : to store a string
len : length of the given string.
Member functions
Mystring() : constructor
void readstring() : reads the given string from input.
int code(int index) : returns Unicode for character at position index.
void word() : displays the longest word in the string.
Specify the class Mystring giving details of the constructor and void readstring(), int
code(int index), void word() only. The main function need not be written.
Question 2008.
A class Modify has been defined with the following details:
Class name : Modify
Data members
st : stores a string
len : to store the length of the string.
Member functions
void read() : to accept the string in uppercase alphabets.
void putin(int , char) : to insert a character at the specified position in the string and
display the changed string.
void takeout(int) : to remove character from the specified position in the string and
display the changed string.
void change() : to replace each character in the original string by the character
which is at a distance of 2 moves ahead.
For example:- “ABCD” becomes “CDEF”, “XYZ” becomes “ZAB”.
Specify the class Modify giving details of the functions vod read(), void putin(int,char),
void takeout(int) and void change().
The main function need not be written.
Page | 5
UMESH COMPUTER KENDRA (PH: 9897188951)
Question 2009.
A class SortWord has been defined with the following details:
Class name : SortWord
Data members
txt : stores the word.
len : stores the length of the word.
Member functions
SortWord() : default constructor
voidreadTxt() : to accept the word in lower case.
voidsortTxt() : to sort the word in alphabetical order of characters using bubble sort
technique and display it.
voidchangeTxt() : to change the case of vowels in the word toupper case.
For example :- school becomes schOOl.
void disp() : to display the changed string.
Specify the class SortWord giving the details of the functions constructor, void readTxt(),
void sortTxt(), void changeTxt() and void disp(). You need not write the main function.
Question 2011.
Input a sentence and count the number of times, the words “an” and “and” are present
in the sentence. Design a class Frequency using the description given below:
Class name : Frequency
Data Members
text : stores the sentence
countand : to store the frequency of the word and.
countan : to store the frequency of the word an.
len : stores the length of the string.
Member functions
Frequency() : constructor to initialize the data variables.
void accept(String n) : to assign n to text where the value of the parameter should be
in lowercase.
void checkandfreq() : to count the frequency of and.
void checkanfreq() : to count the frequency of an.
void display() : to display the frequency of “an” and “and” with suitable msg.
Page | 6
UMESH COMPUTER KENDRA (PH: 9897188951)
Specify class Frequency giving details of the constructor(), void accept(String), void
checkandfreq() and void display(). Also define the main function to create an object and
call methods accordingly to enable the task.
Question 2010.
Input a word in uppercase and check for the position of the first occurring vowel and
perform the following operation.
i) Words that begin with a vowel are concatenated with “Y” . For example, EUROPE
becomes EUROPEY.
ii) Words that contain a vowel in between should have the first part from the position
of the vowel till end, followed by the part of the string from beginning till position of the
vowel and is concatenated by “C”. For example PROJECT becomes OJECTPRC.
iii) Words which do not contain a vowel are concatenated with “N”. For example, SKY
becomes SKYN.
Design a class Rearrange using the description of the data members and member
functions given below:
Class name : Rearrange
Data members
Txt : to store a word
Cxt : to store the rearranged word
len : to store the length of the word
Member functions
Rearrange() : constructor to initialize the instance variables
void readword() : to accept the word input in UPPERCASE
void convert() : converts the word into its changed form and stores it in string Txt
void display() : displays the original and the changed word
Specify the class Rearrange giving the details of the constructor(), void readword(), void
convert() and void display(). Define a main function to create an object and call the
function accordingly to enable the task.
Page | 7
UMESH COMPUTER KENDRA (PH: 9897188951)
Question 2012.
Design a class VowelWord to accept a sentence and calculate the frequency of words
that begin with a vowel. The words in the input string are separated by a single blank
space and terminated by a full stop.
The description of the class is given below:
Class Name : VowelWord
Data members
str : to store a sentence
freq : to store the frequency of words beginning with a vowel.
Member functions
VowelWord() : constructor to initialize data members to legal initial values.
voidreadstr() : to accept a sentence.
voidfreq_vowel( ) : counts the frequency of the words beginning with a vowel.
void display() : to display the original string and the frequency of the words that
begin with a vowel.
Specify the class VowelWord giving details of the constructor( ), void readstr(), void
freq_vowel() and void display(). Also defing the main function to create an object and
call the methods accordingly to enable the task.
Question 2013.
Design a class Exchange to accept a sentence and interchange the first alphabet with the
last alphabet for each word in the sentence, with single letter word remaining
unchanged. The words in the input are separated by a single blank space and terminated
by a full stop.
Example: Input: It is a warm day.
Ouput: tI si a marw yad
Some of the data members and member functions are given below:
Class Name : Exchange
Data members
sent : stores the sentence
rev : to store the new sentence
size : stores the length of the sentence
Member functions
Exchange() : default constructor
Page | 8
UMESH COMPUTER KENDRA (PH: 9897188951)
void readsentence() : to accept the sentence
void exfirstlast() : extract each word and interchange the first and last alphabet of
the word and form a new sentence.
void display() : display the original sentence along with the new changed
sentence.
Specify the class Exchange giving details of the constructor(), void readsentence(), void
exfirstlast() and void display(). Define the main() function to create an object and call
the functions accordingly to enable the task.
Question 2014.
A sequence of Fibonacci strings is generated as follows:
S0 =”a”, S1=”b”, Sn=S(n-1)+S(n-2) where ‘+’ denotes concatenation. Thus the sequence
is: a, b, ba, bab, babba, babbabab, ........ n terms.
Design a class FiboString to generate Fibonacci strings. Some of the members of
the class are given below:
Class name : FiboString
Data members
x: to store the frst string
y: to store the second string
z: to store the concatenation of the previous two strings
n: to store the number of terms
Member methods
FiboString() : constructor to assign x=”a”, y=”b” and z=”ba”
void accept() : to accept the number of terms ‘n’
void generate() : to generate and print the Fibonacci strings. The sum of
(‘+’ i.e. concatenation) first two strings is the third string.
Eg. “a” is first string, “b” is second string then the third will be
“ba”, and fourth will be “bab” and so on.
Specify the class FiboString, giving details of the constructor(), void accept() and void
generate(). Define the main() function to create an object and call the functions
accordingly to enable the task
Page | 9
UMESH COMPUTER KENDRA (PH: 9897188951)
Question 2015.
A class TheString accepts a string of a maximum of 100 characters with only one blank
space between the words. Some of the members of the class are as follows:-
Class name : TheString
Data members
str : to store a string
len : integer to store the length of a string
wordcount : integer to store the number of words
cons : integer to store the number of consonants
Member functions
TheString() : default constructor to initialize the data members
TheString(String ds) : parameterized constructor to assign str=ds
voidcountFreq() : to count the number of words and number of consonants and
store them in wordcount and cons respectively.
void Display() : to display the original string, along with the number of words and
the number of consonants.
Specify class TheString giving details of the constructors, void countFreq() and void
Dsiplay(). Define the main( ) function to create an object and call the functions
accordingly to enable the task.
Page | 10
UMESH COMPUTER KENDRA (PH: 9897188951)
Question P2012
Write a program to accept a sentence as input. The words in the string are to be
separated by a blank. Each word must be in upper case. The sentence is terminated by
either '.' , '!' or '?'. Perform the following tasks:
Test your program with the sample data and some random data:
Example 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
Page | 11
UMESH COMPUTER KENDRA (PH: 9897188951)
Question P2013.
A palindrome is a word that may be read the same way in either direction. Accept a
sentence in UPPERCASE which is terminated by either “.”, “?”, or “!”. Each word of the
sentence is separated by a single blank space.
Perform the following tasks:
a) Display the count of palindromic words in the sentence.
b) Display the palindromic words in the sentence
Example of palindromic words:
MADAM, ARORA, NOON
Test your program with the sample data and some random data:
Example 1
INPUT : MOM AND DAD ARE COMING AT NOON.
OUTPUT : MOM DAD NOON
NUMBER OF PALINDROMIC WORDS : 3
Example 2
INPUT : NITIN ARORA USES LIRIL SOAP.
OUTPUT : NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS : 3
Example 3
INPUT : HOW ARE YOU?
OUTPUT : NO PALINDROMIC WORD
Page | 12
UMESH COMPUTER KENDRA (PH: 9897188951)
Question P2017.
Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13
places’). It is a simple letter substitution cipher that replaces a letter with the letter 13
places after it in the alphabets, with the other characters remaining unchanged.
ROT13
A/a B/b C/c D/d E/e F/f G/g H/h I/I J/j K/k L/l M/m
↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕ ↕
N/n O/o P/p Q/q R/r S/s T/t U/u V/v W/w X/x Y/y Z/z
Write a program to accept a plain text of length L, where L must be greater than 3 and
less than 100. Encrypt the text if valid as per the Caesar Cipher.
Test your program with the sample data and some random data:
Example 1
INPUT: Hello! How are you?
OUTPUT: The cipher text is:
Uryyb? Ubj ner lbh?
Example 2
INPUT: Encryption helps to secure data.
OUTPUT: The cipher text is:
Rapelcgvba Urycf gb frpher gngn.
Example 3
INPUT: You
OUTPUT: INVALID LENGTH
Page | 13
UMESH COMPUTER KENDRA (PH: 9897188951)