Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Created March 4, 2017 18:27
Show Gist options
  • Save shailrshah/819c3c3bfd738c4392a23f8afda24728 to your computer and use it in GitHub Desktop.
Save shailrshah/819c3c3bfd738c4392a23f8afda24728 to your computer and use it in GitHub Desktop.
/**
* Created by shail on 03/04/17.
*/
public class Likes {
public static String likes1(String[] people){
if(people.length == 0)
return "No one likes this";
else if(people.length == 1)
return people[0] + " likes this";
else {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < people.length - 2 ; i++)
sb.append(people[i] + ", ");
sb.append(people[people.length-2] + " ");
sb.append("and " + people[people.length-1] + " like this");
return sb.toString();
}
}
public static void main(String args[]){
//empty array
System.out.println(likes1(new String[] {}));
//One element in array
System.out.println(likes1(new String[] {"Shail"}));
//Two elements in array
System.out.println(likes1(new String[] {"Shail", "Priyanka"}));
//Three elements in array
System.out.println(likes1(new String[] {"Shail", "Priyanka", "Kalpita"}));
//Four elemnets in array
System.out.println(likes1(new String[] {"Shail", "Priyanka", "Kalpita", "Rabab"}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment