import java.util.Scanner; public class replaceFirst { public static String replaceFirst(String Str,char c) { if(Str == null) return null; else if(Str == "") return ""; else { Str = Str.substring(0,0) + c + Str.substring(0 + 1);// modified string return Str; } } public static void main(String[] args) { System.out.print( replaceFirst("basant",'c')); } }
How do I have my code output answers like:
s = MidtermProblems.replaceFirst(null, 't');
s = MidtermProblems.replaceFirst("", 't');
s = MidtermProblems.replaceFirst("lice", 'm');
s = MidtermProblems.replaceFirst("a", 't');
import java.util.Scanner;
public class replaceFirst
{
public static String replaceFirst(String Str,char c)
{
if(Str == null)
return null;
else if(Str == "")
return "";
else
{
Str = Str.substring(0,0) + c + Str.substring(0 + 1);// modified string
return Str;
}
}
public static void main(String[] args)
{
System.out.print( replaceFirst("basant",'c'));
}
}
Step by step
Solved in 4 steps with 1 images