Stack Membalik Kata
#include <iostream.h>
#include <conio.h>
struct stack{
int top;
char kata[10];};
stack s;
void inisialisasi()
{s.top=-1;}
void push(char data)
{s.top ++;
s.kata[s.top] = data;}
void pop()
{s.kata[s.top] = NULL;
s.top --;}
void print()
{ for (int i=s.top;i>-1;i--)
{cout<<s.kata[i];} }
main()
{ char data[20];
int panjang_kata;
cout<<"kata ke-1 : "<<data;
cin>>data;
panjang_kata = strlen(data);
inisialisasi();
for (int i=0;i<panjang_kata;i++)
{push (data[i]);}
cout<<"kata ke-1 dibalik menjadi : ";
print();
cout<<endl<<"================================================="<<endl;
char data2[20];
int panjang_kata2;
cout<<"kata ke-2 : "<<data2;
cin>>data2;
panjang_kata2=strlen(data2);
inisialisasi();
for (int i=0;i<panjang_kata2;i++)
{push (data2[i]);}
cout<<"kata ke2 dibalik menjadi : ";
print();
cout<<endl<<"================================================="<<endl;
char data3[20];
int panjang_kata3;
cout<<"kata ke-3 : "<<data3;
cin>>data3;
panjang_kata3=strlen(data3);
inisialisasi();
for (int i=0;i<panjang_kata3;i++)
{push (data3[i]);}
cout<<"kata ke3 dibalik menjadi : ";
print();
cout<<endl<<"================================================="<<endl;
getch();
}
Comments
Post a Comment