Contoh:
Input
: malam
Output
pada kamuskata.txt : malam m=2 a=2 l=1 Palindrom.#include <iostream> #include <ctype.h> #include <fstream> #include<cstring> using namespace std; ifstream baca; ofstream tulis; int jum_huruf(char str[1000]){ unsigned int a,b,c,index[100],jumlah[100],tampung; for(a=0; str[a]; a++){ tampung=0; if(!isspace(str[a])){ for(b=0; str[b]; b++){ if(str[b]==str[a]){ tampung++; jumlah[a]=tampung; index[b]=a; } } } } for(c=0; str[c]; c++){ if(c==index[c]){ cout<<"karakter "<<str[c]<<" = "<<jumlah[c]<<endl; tulis<<"karakter "<<str[c]<<" = "<<jumlah[c]<<endl; } } } int main(){ baca.open("D:/kamuskata.txt", ios::app); char str[100]; int belakang; bool palindrome = true; while(baca.fail() ){ cout<<"file tidak dapat dibuka"<<endl; return 1; } cout<<"Masukkan kalimat = "; cin.getline(str,sizeof(str)); tulis.open("D:/kamuskata.txt",ios::trunc); tulis<<"Input : "<<str<<"\n"; jum_huruf(str); int panjang = strlen(str); belakang = panjang-1; for(int a=0; a<panjang; a++) { if((char)tolower(str[a])!=(char)tolower(str[belakang])) { palindrome = false; break; } belakang--; } if(palindrome) { cout<<"Palindrom"<<endl; tulis<<"\nPalindrom"<<endl; } else { cout<<"Bukan Palindrom"<<endl; tulis<<"\nBukan Palindrom"<<endl; } baca.close(); tulis.close(); return 0; }
0 comments:
Post a Comment