Loading...
Sunday, December 22, 2013

Tugas Basis Data Kelas 3 C

/*soal nomor 1*/
DELIMITER//
DROP FUNCTION IF EXISTS vokal//
CREATE FUNCTION vokal(kata VARCHAR(255)) RETURNS INT
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE hasil1 INT DEFAULT 0;
WHILE i<= LENGTH(kata) DO
IF LOWER(SUBSTRING(kata,i,1)) = 'a' THEN
SET hasil1 = hasil1+1;
END IF;
IF LOWER(SUBSTRING(kata,i,1)) = 'i' THEN
SET hasil1 = hasil1+1;
END IF;
IF LOWER(SUBSTRING(kata,i,1)) = 'u' THEN
SET hasil1 = hasil1+1;
END IF;
IF LOWER(SUBSTRING(kata,i,1)) = 'e' THEN
SET hasil1 = hasil1+1;
END IF;
IF LOWER(SUBSTRING(kata,i,1)) = 'o' THEN
SET hasil1 = hasil1+1;
END IF;
SET i = i+1;
END WHILE;
RETURN hasil1;
END//

SELECT vokal('pendidikan teknik informatika');

/*solan nomor 2*/
DELIMITER//
DROP FUNCTION IF EXISTS tukar_gotik//
CREATE FUNCTION tukar_gotik(kata VARCHAR(255)) RETURNS VARCHAR(255)
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT;
DECLARE hasil VARCHAR(255);
DECLARE hasil1 VARCHAR(255) DEFAULT '';
DECLARE hasil2 VARCHAR(255) DEFAULT '';
SET j = LENGTH(kata);
WHILE i<= LENGTH(kata) DIV 2 DO
SET hasil1 = CONCAT(hasil1,SUBSTRING(kata,i+1,1),SUBSTRING(kata,i,1));
SET hasil2 = CONCAT(SUBSTRING(kata,j,1),SUBSTRING(kata,j-1,1),hasil2);
SET i = i+2;
SET j = j-2;
END WHILE;

IF LENGTH(kata) MOD 2 = 1 THEN
SET hasil = CONCAT(hasil1,SUBSTRING(kata,i,1),hasil2);
ELSE
SET hasil = CONCAT(hasil1,hasil2);
END IF;
RETURN hasil;
END//

SELECT tukar_gotik('JANURBAWA');


/*soal nomor 3*/
DELIMITER//
DROP FUNCTION IF EXISTS tukar_abjad//
CREATE FUNCTION tukar_abjad(kata VARCHAR(255)) RETURNS VARCHAR(255)
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT;
DECLARE hasil VARCHAR(255) DEFAULT '';

WHILE i<= LENGTH(kata)-2 DO
SET hasil = CONCAT(hasil,SUBSTRING(kata,i+2,1),SUBSTRING(kata,i+1,1),SUBSTRING(kata,i,1));
SET i = i+3;
END WHILE;
RETURN hasil;
END//

SELECT tukar_abjad('ABCDEF');

/*soal nomor 4*/
DELIMITER//
DROP FUNCTION IF EXISTS sorting//
CREATE FUNCTION sorting(kata VARCHAR(255)) RETURNS VARCHAR(255)
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT DEFAULT 2;
DECLARE temp INT;
DECLARE hasil VARCHAR(255) DEFAULT '';

WHILE i < LENGTH(kata) DO
SET temp = CAST(SUBSTRING(kata,i,1) AS INT);
WHILE j<= LENGTH(kata) DO
IF temp > CAST(SUBSTRING(kata,j,1) AS INT) THEN
SET temp = CAST(SUBSTRING(kata,j,1) AS INT);
END IF;
SET j = j+1;
END WHILE;
SET hasil = CONCAT(hasil,CAST(temp AS CHAR(1)));
SET i = i+1;
SET j = i+1;
END WHILE;
RETURN hasil;
END//

SELECT tukar_abjad('257913');
SELECT CAST('257913' AS BINARY);

0 comments:

Post a Comment

 
TOP