-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatFirst
More file actions
29 lines (25 loc) · 775 Bytes
/
atFirst
File metadata and controls
29 lines (25 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*Given a string, return a string length 2 made of its first 2 chars. If the string length is less than 2, use '@' for
the missing chars.
atFirst("hello") → "he"
atFirst("hi") → "hi"
atFirst("h") → "h@"
*/
package string1;
public class atFirst {
public static void main(String[] args) {
String output=atFirst("hello");
System.out.println(output);
String output1=atFirst("hi");
System.out.println(output1);
String output2=atFirst("h");
System.out.println(output2);
}
public static String atFirst(String str) {
if ((str.length()>=2))
return str.substring(0,2);
else if ((str.length()==1))
return str.substring(0,1)+"@";
else
return "@@";
}
}