C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Function Overloading - String Based Program - Java Programs

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)

class Overload{ public static void joyString(String s, char ch1, char ch2){ s = s.replace(ch1, ch2); System.out.println(s); } public static void joyString(String s){ int first = s.indexOf(' '); int last = s.lastIndexOf(' '); System.out.println("First index: " + first); System.out.println("Last index: " + last); } public static void joyString(String s1, String s2){ String s3 = s1 + " " + s2; System.out.println(s3); } }

Output