0% found this document useful (0 votes)
21 views2 pages

Contoh Program String

Program string

Uploaded by

Abdul Butun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Contoh Program String

Program string

Uploaded by

Abdul Butun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Contoh-contoh program olah string

1. Penggabungan string(concatenation)

package olahstring1;
public class OlahString1 {

public static void main(String[] args) {


String namadepan="Ahmad";
String namabelakang="Fauzi";
String nama=namadepan+namabelakang;
System.out.println("Halo "+nama+" apa kabar? ");
System.out.println(namadepan.concat(namabelakang)+"anda memang pemuda gentle ");
}

}
2. Pengurutan string
package olahstring2;
public class OlahString2 {

public static void main(String[] args) {


int i,j;
String nama[]={"Agi","Adi","Ari"},tamp;
System.out.println("sebelum diurut : ");
System.out.println("=================");
for(i=0;i<nama.length;i++)
System.out.print(nama[i]+" ");
System.out.println("");
System.out.println("\nSetelah diurut : ");
System.out.println("==================");
for(i=0;i<(nama.length-1);i++){
for(j=(i+1);j<nama.length;j++)
if(nama[j].compareTo(nama[i])<1){
tamp=nama[j];
nama[j]=nama[i];
nama[i]=tamp;
}
}
for(i=0;i<nama.length;i++)
System.out.print(nama[i]+" ");
System.out.println("");
}

}
3. Membalik urutan
package olahstring3;
import java.util.Scanner;
public class OlahString3 {

public static void main(String[] args) {


String kata;
System.out.print("Ketikkan kalimat sembarang : ");
Scanner input=new Scanner(System.in);
kata=input.nextLine();
StringBuffer balik;
balik=new StringBuffer(kata);
System.out.println(kata);
System.out.println(balik.reverse());
}

}
4. Mengambil beberapa karakter

package olahstring5;
public class OlahString5 {
public static void main(String[] args) {
int i;
String teks="SOTO BANDUNG",teksmu="";
for(i=1;i<teks.length();i++)
teksmu+=teks.charAt(i);
System.out.println(teksmu);
}

You might also like