1/02/2013

Contoh Program C++ (Menggabungkan String)

//strcat.cpp
#include 
#include 
#include 
#include 
#include 

int main(void)
{
char a1[20];
char a2[20];

clrscr();
cout << "Masukkan Kata ke-1 = ";
cin >> a1;

cout << "Masukkan Kata ke-2 = ";
cin >> a2;

strcat(a1, a2);

cout << "Hasil Penggabungannya " << a1;
getch();
return 0;
}
Membandingkan String (Case Sensitive)

//strcmp.cpp
#include 
#include 
#include 
#include 

int main(void)
{
char a1[] = "STRING";
char a2[] = "String";
char b1[] = "sTrInG";

clrscr();
cout << "Hasil Perbandingan " << a1 << " dan ";
cout << a2 << "->";
cout << strcmp(a1, a2) << endl;

cout<< "Hasil Perbandingan " << a2 << " dan ";
cout << a2 << "->";
cout << strcmp(a2,a1) << endl;

cout << "Hasil Perbandingan " << a1 << " dan ";
cout << b1 << "->";
cout << strcmp(a1,b1) << endl;

getch();
return 0;
}
Menyalin String

//strcpy.cpp
#include 
#include 
#include 
#include 

int main(void)
{
char huruf[20];
char pindah[20];

clrscr();
cout << "Masukkan Sembarang Kata = ";
gets(huruf);

strcpy(pindah, huruf);

cout << "Penyalinannya = " << pindah;
getch();
return 0;
}
Menghitung Panjang String

//strlen.cpp
#include 
#include 
#include 
#include 

int main(void)
{
char huruf[20];

clrscr();
cout << "Masukkan Sembarang Kata = ";
gets(huruf);

cout << "Panjang Kata Yang Diinputkan = ";
cout << strlen(huruf);
getch();
return 0;
}
Membalik Urutan Karakter

//strrev.cpp
#include 
#include 
#include 
#include 

int main(void)
{
char kata[20];

clrscr();
cout<<"Masukan Sembarang Kata = ";
gets(kata);

strrev(kata);

cout << "Hasil Perubahan = " << kata;
getch();
return 0;
}
Mencari sebuah karakter dalam string
//strchr.cpp
#include
#include
void main(){
char *hasil;
char teks[] = "ABCD";
char karakter = 'B';
hasil = strchr(teks,karakter);
if (hasil != NULL)
{ cout << "Ada" << endl;
cout << "Isi hasil : " << hasil;}
else
{ cout << "Tidak ada" << endl;
cout << "Isi hasil : " << hasil;}
getch();}

Contoh Aplikasi Sederhana

//contoh .cpp
#include 
#include 
#include 
#include 

typedef unsigned long int ULONG;

int main(void)
{
char nim[9], nama[35], jurusan[25];
char kursus[30], nopeserta[9];
char bonus[30], kdj[3], kode;

ULONG biaya = 0;

form:
clrscr();

cout << " AKADEMI BINTANG TERANG \n";
cout << " FORMULIR KURSUS MAHASISWA D1 \n";
cout << "___________________________________\n\n";
cout << "Kode Kursus: 1. Visual Basic 6.0\n";
cout << " 2. Visual Foxpro 9.0\n";
cout << " 3. Borland Delphi 7\n";
cout << " 4. Linux (Basic User)\n";
cout << " 5. Linux (Networking)\n";
cout << " 6. PHP + MySQL\n\n";
cout << " 0. Hentikan Program\n";
cout << "___________________________________\n\n";
cout << "Pilihan [1/2/3/4/5/6]: ";

kode = getche();

if ((kode != '1') && (kode != '2') && (kode != '3') &&
(kode != '4') && (kode != '5') && (kode != '6') &&
(kode != '0'))
goto form;

switch (kode)
{
case '1' :
strcpy(kursus, "Visual Basic 6.0");
strcpy(bonus, "Flash Disk 1 GB");
biaya = 150000;
break;

case '2' :
strcpy(kursus, "Visual Foxpro 9.0");
strcpy(bonus, "Flash Disk 1 GB");
biaya = 150000;
break;

case '3' :
strcpy(kursus, "Borland Delphi 7");
strcpy(bonus, "CD Borland Turbo Explorer");
biaya = 350000;
break;

case '4' :
strcpy(kursus, "Linux (Basic User)");
strcpy(bonus, "CD Linux IGOS");
biaya = 150000;
break;

case '5' :
strcpy(kursus, "Linux (Networking)");
strcpy(bonus, "CD Linux Fedora Core 5");
biaya = 350000;
break;

case '6' :
strcpy(kursus, "PHP + MySQL");
strcpy(bonus, "CD Master Web Magazine");
biaya = 200000;
break;

case '0' :
return 0;
break;
}

cout << "\n\n";
cout << "Nomor Induk Mahasiswa : ";
scanf("%[^\n]", nim);
fflush(stdin);

cout << "Nama Mahasiswa : ";
scanf("%[^\n]", nama);
fflush(stdin);

strcpy(nopeserta, nim);
strrev(nopeserta);

strncpy(kdj, nim, 2);
kdj[2] = '\0';

strupr(nama);

if (strcmp(kdj, "11") == 0)
strcpy(jurusan, "Komputer Akuntansi");

if (strcmp(kdj, "12") == 0)
strcpy(jurusan, "Manajemen Informatika");

if (strcmp(kdj, "13") == 0)
strcpy(jurusan, "Teknik Komputer");

strupr(jurusan);
strupr(kursus);
strupr(bonus);

clrscr();
cout << " DATA KURSUS PROGRAM STUDI D1 \n";
cout << " AKADEMI BINTANG TERANG \n";
cout << "__________________________________\n\n";
cout << "Nomor Induk Mahasiswa : " << nim << endl;
cout << "Nama Lengkap : " << nama << endl;
cout << "Jurusan : " << jurusan << endl;
cout << "Nomor Peserta Kursus : " << nopeserta << endl;
cout << "Kode Kursus : " << kode << endl;
cout << "Program Kursus : " << kursus << endl;
cout << "Biaya Kursus Rp. : " << biaya << endl;
cout << "Bonus : " << bonus << endl;
cout << "__________________________________\n\n";

getch();
goto form;
}

Sumber

Comments
0 Comments

0 comments:

Post a Comment

Tolong tinggalkan sedikit komentar untuk koreksi blog ini.
Harap tidak memberikan komentar :
1. Sara dan pornografi
2. Spam Content (Konten Sampah)
3. Penghinaan dan Pelecehan
4. OOT (Out Of Topic)