Jumaat, 8 April 2016

Program membalik kata C++ interatif& rekursif #prak 4

Interatif C++
#include <iostream>
#include<string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
    char kata[100];
    int bnyk;
    cout<<"Kata awal : ";cin>>kata;
    bnyk=strlen(kata);
    cout<<"Kata setelah di balik : ";
    for(bnyk=strlen(kata)-1;bnyk>=0;bnyk=bnyk-1){
        cout<<kata[bnyk];
    }
    return 0;
}
Raptor interatif

Rekursif C++

#include <iostream>
#include <cstdlib>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
char str[128];
char pil;
void balik(int jumlah, char string[]){
cout<<string[jumlah];
if(jumlah>0){
balik((jumlah-1),string);
}
}



int main(int argc, char** argv) {
cout<<"masukkan kata = ";
cin.getline(str,sizeof(str));
cout<<endl;
cout<<"kata menjadi setelah di balik = ";
balik((cin.gcount()-2),str);
return 0;
}

Raptor rekursif

Load disqus comments

0 ulasan