You are on page 1of 3

LAB TEST 4

[Question - LT4 - Find the last a]


Write a program to read a string. Your program should output the last location of
letter "a" in the string.
Sample input:
manusia
Sample output:
Last a is found at location 7
import java.util.Scanner;
public class syukriah{
public static void main(String[] args) {
String ayat;

}
}

Scanner in = new Scanner(System.in);


ayat=in.nextLine();
int index=ayat.lastIndexOf('a')+1;
System.out.println("Last a is found at location "+index);

[Question - LT4 - Four First and Last]


Write a program to read 4 strings. Your program should take the first and last
letter of each string and combine them together as a single output as shown in
the sample output.
Sample input:
lembu
unta
kambing
ayam
Sample output:
lu-ua-kg-am

import java.util.Scanner;
public class syukriah {
public static void main(String[] args)
{
int length;
String ayat[]=new String[10];

Scanner in = new Scanner(System.in);


for(int i=0;i<4;i++)
{
ayat[i]=in.nextLine();
length=ayat[i].length()-1;
//length=length-1;
System.out.print(ayat[i].charAt(0));
System.out.print(ayat[i].charAt(length));
if(i!=3)
System.out.print("-");
}

}
}

[Question - LT4 - Search The Last]


Write a program to read 4 strings. For each string, your program should identify
either the string last with "ng" or not. Output your result as shown in Sample
Output (yes if the string ending with ng and no if not).
Sample input:
seladang
unta
kambing
ayam
Sample output:
yes
no
yes
no

import java.util.Scanner;
public class syukriah {
public static void main(String[] args) {
String text;
int i;
Scanner in = new Scanner(System.in);
for(i=1;i<=4;i++)
{
text=in.nextLine();
if (text.endsWith("ng"))
System.out.println("yes");
else
System.out.println("no");
}
}
}

You might also like