top of page
Writer's pictureD A Rosi Arsida Wardani

Latihan

Updated: May 27, 2021

Soal. Algoritma dan fungsi menghitung panjang string

Jawab :

Algoritma panjang_string

{membaca karakter yang dimasukkan untuk kemudian dihitung panjang stringnya}

Deklarasi :

i, hasil : integer

kar : array [100] of char

Deskripsi :

gets (kar)

function karakter (input kar : array [100] of char) : integer

hasil ← 0

for i ← 0 to kar [i] != '/0' do

if (kar [i] != '\0') then

hasil ← hasil + 1

end if

end for

write (hasil)

end function

write (karakter (char kar [100]))


Program C

#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;

class hitung_karakter{
	public :
		int i, hasil;
		char kar[100];
		input();
		karakter(char kar[100]);
};

hitung_karakter::input(){
	cout<<"masukkan karakter/kalimat\n";
    gets(kar);
}

int hitung_karakter::karakter(char kar[100]){
		hasil=0;
        for(i=0; kar[i] != '\0'; i++){
        	if(kar[i]!='\0'){
            	hasil++;
        	}
    	}
    	cout<<"jumlah karakternya yaitu "<<hasil;
}	

int main(){
	hitung_karakter x;
	char kar[100];
	x.input();
	x.karakter(kar);

	return 0;
}

Hasil run :


 

Soal. Algoritma mengubah string menjadi huruf besar dan kecil secara bergantian

Jawab :

Algoritma mengubah_string

{mengubah string dari kalimat yang diinputkan menjadi bergantian huruf besar dan kecil}

Deklarasi :

j, i : integer

kalimat : array [1000] of char

Deskripsi :

j ← 0

n ← 1

read kalimat

if ((kalimat [0] >= 'a') And (kalimat [0] <='z')) then

kalimat [0] ← kalimat [0] - 32

end if

for i ← 0 to strln (kalimat) do

if (i % 2 = 0) then

if ((kalimat [n] >= 'A') And (kalimat [n] <= 'Z')) then

kalimat [n] ← kalimat [n] ← kalimat [n] + 32

end if

n ← n + 1

end if

end for

for i ← 1 to kalimat [i] != '/0' do

if (kalimat [i] = ' ') then

j ← j + 1

else if ((i - 1 - j) % 2 = 0) then

if (kalimat [i + 1] >= 'a' And kalimat [i + 1] <= 'z') then

kalimat [i + 1] ← kalimat [i + 1] - 32

end if

end if

end for

write (kalimat)


Program C++


#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;

class mengubah_string{
	public :
		int j=0;
		int n=1;
		char kalimat[1000];
		input();
		proses();
		cetak();
	private :
		int i;	
};

mengubah_string::input(){
	cout<<"masukkan kata/kalimat : ";
	cin.getline(kalimat, 1000);
}

mengubah_string::proses(){
	if((kalimat[0]>='a')&& (kalimat[0]<='z')){
		kalimat[0]=(char)kalimat[0]-32;
	}
	
	for (i=0;i<(int)strlen(kalimat);i++){
    		if (i%2==0){
			if ((kalimat[n]>='A')&& (kalimat[n]<='Z')){
				kalimat[n]=(char)kalimat[n]+32;
			}
			n++;
		}
	}

	for (i=1; kalimat[i]!='\0'; i++){
    	if (kalimat[i]==' '){
    		j++;	
		}else if ((i-1-j)%2==0){
			if(kalimat[i+1]>='a' && kalimat[i+1]<='z'){
				kalimat[i+1]=(char)(kalimat[i+1]-32);
			}
		}
	}
}

mengubah_string::cetak(){
	cout<<"output : "<<kalimat<<endl;
}

int main(){
	mengubah_string x;
	
	x.input();
	x.proses();
	x.cetak();

	return 0;
}

Hasil run :


8 views0 comments

Recent Posts

See All

One-dimensional Arrays

One-dimensional arrays are data structures that contain data types of the same type. In the form of a group of related memories...

Loop

Introduction Examples of algorithms in everyday life : Example 1 : To finish eating a plate of rice (initial conditions) Mouthfuls of a...

Comentários


Post: Blog2_Post
bottom of page