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: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:
s = “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 strings
with a space between them and prints the resultant string.
Example:
INPUT:
s1 = “COMMON WEALTH”
s2 = “GAMES”
OUTPUT:
COMMON WEALTH GAMES
(use library functions)
Program
{
void joyString(String s, char ch1, char ch2)
{
s = s.replace(ch1, ch2);
System.out.println(s);
}
void joyString(String s)
{
int first = s.indexOf(' ');
int last = s.lastIndexOf(' ');
System.out.println("First index of space is: " + first);
System.out.println("Last index of space is: " + last);
}
void joyString(String s1, String s2)
{
String s= s1.concat(" ");
s= s.concat(s2);
System.out.println(s);
}
public static void main(String args[])
{
Overloading ob = new Overloading();
ob.joyString("computer",'o','i');
ob.joyString("topicwise computer icse questions");
ob.joyString("computer","programming");
}
}
No comments:
Post a Comment