(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 JJoystring(str1,ch1,ch2);
break;
}oystring (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)
import java.util.*;
class Ab
{
String str1,str2;
char ch1,ch2;
int choice;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter your choice (1/2/3 for 1st function, 2nd function and 3rd function: ");
choice=sc.nextInt();
sc=new Scanner(System.in);
switch(choice)
{
case 1:
System.out.print("\nEnter the sentence:");
str1=sc.nextLine();
System.out.print("\nEnter the 1st Character:");
ch1=sc.next().charAt(0);
System.out.print("\nEnter the 2nd Character:");
ch2=sc.next().charAt(0);
Joystring(str1,ch1,ch2);
break;
case 2:
System.out.print("\nEnter the sentence:");
str1=sc.nextLine();
Joystring(str1);
break;
case 3:
System.out.print("\nEnter 1st sentence:");
str1=sc.nextLine();
System.out.print("\nEnter 2nd sentence:");
str2=sc.nextLine();
Joystring(str1,str2);
break;
}
}
private void Joystring(String s, char ch1, char ch2)
{
s=s.replace(ch1,ch2);
System.out.print("\nModified sentence="+s);
}
private void Joystring(String s)
{
int i=s.indexOf(' ');
System.out.print("\nFirst Space Location="+i);
i=s.lastIndexOf(' ');
System.out.print("\nLast Space Location="+i);
}
private void Joystring(String s,String s1)
{
s=s.concat(" ");
s=s.concat(s1);
System.out.print("\nModified ="+s);
}
}
Sample Input Output
Enter your choice (1/2/3 for 1st function, 2nd function and 3rd function: 1
Enter the sentence:this is a test
Enter the 1st Character:i
Enter the 2nd Character:x
Modified sentence=thxs xs a test
Enter your choice (1/2/3 for 1st function, 2nd function and 3rd function: 2
Enter the sentence:this is a test
First Space Location=4
Last Space Location=9
Enter your choice (1/2/3 for 1st function, 2nd function and 3rd function: 3
Enter 1st sentence:welcome
Enter 2nd sentence:home
Modified =welcome home
No comments:
Post a Comment