Skip to content

Instantly share code, notes, and snippets.

@rmsubekti
Last active November 27, 2016 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmsubekti/99e9b260cd569b53cae234b84acec577 to your computer and use it in GitHub Desktop.
Save rmsubekti/99e9b260cd569b53cae234b84acec577 to your computer and use it in GitHub Desktop.
Mencari sebuah huruf pada sebuah nama
#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