DIKTAT

Minggu, 17 April 2016

diktat bab 7



WORSKHOP

1.     Menyisipkan kata

   Dev c++ :
#include <iostream>
#include <string.h>
#include <cstdlib>
/* 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)
{
          string a,b;int c;
          cout<<"kalimat pertama : ";
          getline(cin, a);
          cout<<"kalimat kedua : ";
          cin>>b;
          cout<<a<<endl;
          cout<<"Menyisipkan kata pada karakter ke : ";cin>>c;
          a.insert(c, b+" ");
          cout<<a;
          return 0;
}


2.     String bergantian huruf besar dan kecil

    Dev c++ :
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
int main()
{
int i,jum;

char string[100];

cout<<"Masukan sembarang kalimat:\n";
cin.getline(string,100); //membaca spasi

//mengubah huruf awal menjadi huruf kapital
if((string[0]>='a')&& (string[0]<='z'))
string[0]=(char)string[0]-32;


//mengubah huruf setelah huruf awal menjadi huruf kecil
jum=1;int d=0;
for(i=0;i<(int)strlen(string);i++)
{
          if(i%2==0){
         
if((string[jum]>='A')&& (string[jum]<='Z'))
string[jum]=(char)string[jum]+32;
jum++;}
}

//mengubah huruf setelah spasi menjadi huruf kapital
for(i=1;string[i]!='\0';i++)
{
          if(string[i]==' ') d++;
if((i-1-d)%2==0)
{
if(string[i+1]>='a' && string[i+1]<='z')
string[i+1]=(char)(string[i+1]-32);
}
}


cout<<"Hasil Akhir : "<<string<<endl;
getch();
}


3. Menghitung Kata Dalam Sebuah Kalimat
    Dev c++ :
#include<stdio.h>
#include<iostream>
#include<ctype.h>
using namespace std;
int main()
{
      char kalimat[100];
      int i, spasi=0;

      cout << "\n\t\t - Program Menghitung Kata Dalam Sebuah Kalimat -\n";
      cout << "\t==================================================================\n\n";
      cout << "\t Masukan Sebuah Kalimat : ";
      cin.getline(kalimat, sizeof(kalimat));

      for(i=0; kalimat[i]; i++)
      {
            if(isspace (kalimat[i]) || ispunct (kalimat[i]))
            {
                  spasi++;
            }
      }
      cout << "\t Jumlah Kata = " << spasi +1 << "\n\n";
      cout << "\t Kata yang anda masukan adalah : \n";
      cout << "\t ";

      for(i=0; kalimat[i]; i++)
      {
            if(isspace (kalimat[i]) || ispunct (kalimat[i]))
            {
                  spasi++;
                  cout << "\n";
                  cout << "\t";
            }
            cout << kalimat[i];
      }

      cout << "\n";
     
      cout << "\t==================================================================\n\n";
      cout << "\n";
      getchar();
}

4. Vektor
    Dev c++ :
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class Vektor {
friend ostream& operator<<(ostream&, Vektor&);
friend istream& operator>>(istream&, Vektor&);
friend class SPL;
public:
Vektor();
void penjumlahan_vektor(const Vektor& A, const Vektor& B);
void perkalian_vektor(float k, const Vektor& A);
void beri_nilaiBanyak(int);
private:
int elemen[100];
int banyak; };
int main(int argc, char** argv) {
         
          return 0;
}

LATIHAN

1.  Buatlah algoritma dan subprogram yang cara kerjanya seperti function pos di atas.
   

2.  Menghitung panjang karakter tanpa strlen
    Dev c++ :
#include <iostream>

/* 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) {
          string a;
          cout<<"Masukkan Kalimat : ";getline(cin,a);
          cout<<a<<endl;
          cout<<"Panjang Karakter : "<<a.size();
          return 0;
}


3. Kapitalisasi huruf dengan inputan kalimat
    Dev 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)
{
          string a;
          cout<<"masukkan kalimat : ";
          getline(cin,a);
          int b = a.length();
          cout<<"dalam bentuk kapital : ";
          for(int c=0;c<=b;c++)
          {
                   a[c]=toupper(a[c]);
                   cout<<a[c];
          }
          cout<<endl;
          cout<<"dalam bentuk huruf kecil : ";
          for(int d=0;d<=b;d++)
          {
                   a[d]=tolower(a[d]);
                   cout<<a[d];
          }
          cout<<endl;
          return 0;
}


Tidak ada komentar:

Posting Komentar