A String variable fullName contains a name in one of two for

A String variable, fullName, contains a name in one of two formats: last name. first name (comma followed by a blank), or first name last name (single blank) Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullName already initialized. You may also declare any other necessary variables.

Solution

package sample;
public class lib {   

public static void main(String args[]) {
  
   int i=0,j=0;
//String fullName=\"cheekati, sandeep\";
String fullName=\"sandeep cheekati\";
String firstName=null;
String lastName=null;
i=fullName.indexOf(\',\');
if(i>0)
{
   lastName=fullName.substring(0,i);
   firstName=fullName.substring(i+2);
}
else
{
   j=fullName.indexOf(\' \');
   firstName=fullName.substring(0,j);
   lastName=fullName.substring(j+1);
}
System.out.println(firstName+\" \"+lastName);
}
}

Explanation :

Try this program with the two possible formats of fullName variable which is in comments.

You can see the output

 A String variable, fullName, contains a name in one of two formats: last name. first name (comma followed by a blank), or first name last name (single blank) E

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site