Question

In: Computer Science

Using SQL, write a table-valued function that: -- takes a space delimited string as input (Input...

Using SQL, write a table-valued function that:

-- takes a space delimited string as input (Input string will be a sentance ex: "The cat is on the chair and the bear is on the chair")

-- returns a table (word varchar(max), count int) (Output is a table that shows how many times each word appeared in the sentance)

Where each space-delimited “word” in the string appears in the table along with the number of times it appeared in the input string.

Additional Instructions: Don’t follow punctuation or language rules; a word is any sequence of non-spaces.

Space-delimited means “all spaces are ignored”, no word may contain spaces

There should not be a length 0 string.

Please include screenshots of the code and output, thanks so much!

Example Input: "The cat is on the chair and the bear is on the chair"

Example Output:

The 4

cat 1

is 2

on 2

chair 2

and 1

bear 1

Note: The user will input the sentence each time. (It is not hard coded)

Solutions

Expert Solution

DECLARE
      str VARCHAR2(100) := 'The cat is on the chair and the bear is on the chair';
      word_string VARCHAR2(40) := '';
      words NUMBER(4) := 1;
      s CHAR;
      current_index NUMBER(4);
      find_word VARCHAR2(40) := '';
      first_space INT(4) := 0;
      table_check INT(4) :=0; 

BEGIN
    SELECT count(*) INTO table_check FROM LETTERS; 
    IF table_check<=0 THEN
      EXECUTE IMMEDIATE 'create table LETTERS (word_letter VARCHAR2(20), frequency INT(4))';
    END IF;
    DELETE FROM LETTERS;
    FOR i IN 1..Length(str) LOOP
      s := Substr(str, i, 1);
      IF s != ' ' THEN
      word_string := CONCAT (word_string, s);
      END IF;
      IF ((s = ' ' or i = Length(str))) THEN
        Select count(*) into find_word from LETTERS where word_letter = lower(word_string);
            IF find_word = 0 then
                INSERT INTO LETTERS (word_letter, frequency) VALUES (lower(word_string), 1);
                word_string := '';
            ELSE
                UPDATE LETTERS set frequency = frequency+1 where word_letter = word_string;
                word_string :='';
            END IF;
      END IF;
END LOOP;

BEGIN
          FOR cursor1 IN (SELECT * FROM LETTERS) 
          LOOP
            dbms_output.put_line(cursor1.word_letter || ' ' || cursor1.frequency);
          END LOOP;
     END;
END;

Output:

Statement processed.
the 4
cat 1
is 2
on 2
chair 2
and 1
bear 1

Attatchments for reference:

Note: When code copied from above, student has to replace [NBSP] with space(' ') in the code, if found any.


Related Solutions

Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a program in Java which will search a space delimited string of integers for the...
Write a program in Java which will search a space delimited string of integers for the two values which add up to a provided amount. The program must output th following text to the command prompt/console: Indexes: { Index of first number} { Index of second number} Example One: NumSerach( 100, " 5 75 25"); output: Indexes: 1 2
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Matlab Create/write a function that takes an input of x and y data, and a string...
Matlab Create/write a function that takes an input of x and y data, and a string (either linear? or quadratic), sets up a linear system of equations (Ax = b), and solves and plots the model.
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT