Program to overload a function joyString(…) and perform the given tasks:
- void joyString(String str,char r,char c) – To replace character with another character in the string.
- void joyString(String str) – To print the first and last index of white space.
- void joyString(String str1,String str2) – To concatenate two string into one using library functions.
Main function not required.
Java
import java.util.*;
public class JoyString
{
public void joyString(String str,char r,char c)
{
str=str.trim();
int l=str.length();
char ch= ' ';
String nstr="";
for(int i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch==r)
{
nstr=nstr+c;
}
else
{
nstr=nstr+ch;
}
}
System.out.println("New String: "+nstr);
}
public void joyString(String str)
{
int l=str.length();
System.out.println("First index of space: "+str.indexOf(' '));
System.out.println("Last index of space: "+str.lastIndexOf(' '));
}
public void joyString(String str1,String str2)
{
str1=str1.trim()+" ";
str2=str2.trim();
String nstr=str1.concat(str2);
System.out.println("Concatenated String: "+nstr);
}
}Java