-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.Menghitung_Kalimat.cpp
39 lines (37 loc) · 970 Bytes
/
12.Menghitung_Kalimat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;
//deklarasi fungsi
int itungKalimat(string x);
int itungKata(string x);
int main() {
//membuat variabel dan pointernya
string kalimat;
string *pointer_kalimat = &kalimat;
cout<<"Masukkan Kalimat Anda = ";
//input beserta spasi
getline(cin,kalimat);
//pemanggilan fungsi
cout<<"Jumlah Kalimat Anda Adalah = "<<itungKalimat(*pointer_kalimat)<<endl;
cout<<"Jumlah Karakter Anda Adalah = "<<itungKata(*pointer_kalimat)<<endl;
}
int itungKalimat(string x){
//inisialisator spasi sebagai 1 kalimat
int spasi = 1;
//foreach kata menjadi char array
for (char cacah : x){
//hitung spasi
if (isspace(cacah,cin.getloc())){
++spasi;
}
}
return spasi;
}
int itungKata(string x){
int karakter = 0;
//foreach kata menjadi array
for (char cacah : x){
//dihitung berapa kali loop
karakter++;
}
return karakter;
}