Count no. of characters and words in a string in PL/SQL Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 4 Likes Like Report Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to count the number of characters and words in the given string. Examples: Input: str = 'Geeks for geeks ' Output: Characters = 13 , Words = 3 Input: str = 'A Computer science portal' Output: Characters = 22, Words = 4 Approach is to maintain two counter variables i.e. one for the characters and the other for words. Start traversing character one by one and increment the count and when there is a blank space then increment the count of words. Below is the required implementation: SQL DECLARE -- Declare required variables str VARCHAR2(40) := 'Geeks for Geeks'; noofchars NUMBER(4) := 0; noofwords NUMBER(4) := 1; s CHAR; BEGIN FOR i IN 1..Length(str) LOOP s := Substr(str, i, 1); -- Count no. of characters noofchars := noofchars + 1; -- Count no. of words IF s = ' ' THEN noofwords := noofwords + 1; END IF; END LOOP; dbms_output.Put_line('No. of characters:' ||noofchars); dbms_output.Put_line('No. of words: ' ||noofwords); END; -- Program End Output : No. of characters:15 No. of words: 3 Create Quiz Comment S Shashank12 Follow 4 Improve S Shashank12 Follow 4 Improve Article Tags : Misc SQL SQL-PL/SQL Explore BasicsWhat is SQL?6 min readSQL Data Types3 min readSQL Operators4 min readSQL Commands | DDL, DQL, DML, DCL and TCL Commands4 min readSQL Database Operations3 min readSQL CREATE TABLE3 min readQueries & OperationsSQL SELECT Query3 min readSQL INSERT INTO Statement4 min readSQL UPDATE Statement3 min readSQL DELETE Statement3 min readSQL - WHERE Clause2 min readAliases in SQL2 min readSQL Joins & FunctionsSQL Joins (Inner, Left, Right and Full Join)4 min readSQL CROSS JOIN1 min readSQL | Date Functions3 min readSQL | String functions6 min readData Constraints & Aggregate FunctionsSQL NOT NULL Constraint2 min readSQL PRIMARY KEY Constraint5 min readSQL Count() Function4 min readSQL SUM() Function2 min readSQL MAX() Function3 min readAVG() Function in SQL2 min readAdvanced SQL TopicsSQL Subquery5 min readWindow Functions in SQL6 min readSQL Stored Procedures7 min readSQL Triggers5 min readSQL Performance Tuning6 min readSQL TRANSACTIONS6 min readDatabase Design & SecurityIntroduction of ER Model9 min readIntroduction to Database Normalization6 min readSQL Injection11 min readSQL Data Encryption5 min readSQL Backup4 min readWhat is Object-Relational Mapping (ORM) in DBMS?7 min read Like