Last active
November 27, 2016 11:16
-
-
Save rmsubekti/99e9b260cd569b53cae234b84acec577 to your computer and use it in GitHub Desktop.
Mencari sebuah huruf pada sebuah nama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <conio.h> | |
using namespace std; | |
//11.12.5800 | |
int main() { | |
//Deklarasi variabel | |
char nama[20], huruf; | |
int jumlah = sizeof(nama) / sizeof(nama[20]), posisi = 0, i = 0; | |
//User menginputkan nama lengkap trmasuk spasi | |
cout << "Masukkan nama : "; cin.getline(nama, jumlah); | |
cout << "Masukkan huruf yang akan dicari : "; cin >> huruf; | |
//Pencarian menggunakan Sequential search | |
while (i< jumlah && tolower(nama[i]) != tolower(huruf)) { | |
i++;//mencari posisi karakter pertama yang sama pada nama | |
} | |
//Cek posisi karakter terakhir yang dicari menggunakan perulangan | |
if (tolower(nama[i]) != tolower(huruf)) { | |
cout << "Maaf huruf " << huruf << " Tidak ditemukan" << endl; | |
} | |
else if (tolower(nama[i]) == tolower(huruf)) { | |
posisi = i + 1; | |
cout << "Huruf ditemukan pada posisi ke " << posisi << endl; | |
} | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment