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 Comment S Shashank12 Follow 4 Improve S Shashank12 Follow 4 Improve Article Tags : Misc SQL SQL-PL/SQL Explore SQL Tutorial 6 min read BasicsWhat is SQL? 6 min read SQL Data Types 3 min read SQL Operators 4 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands 4 min read SQL Database Operations 3 min read SQL CREATE TABLE 3 min read Queries & OperationsSQL SELECT Query 2 min read SQL INSERT INTO Statement 4 min read SQL UPDATE Statement 3 min read SQL DELETE Statement 3 min read SQL - WHERE Clause 3 min read SQL | Aliases 3 min read SQL Joins & FunctionsSQL Joins (Inner, Left, Right and Full Join) 4 min read SQL CROSS JOIN 2 min read SQL | Date Functions 4 min read SQL | String functions 6 min read Data Constraints & Aggregate FunctionsSQL NOT NULL Constraint 2 min read SQL PRIMARY KEY Constraint 5 min read SQL Count() Function 7 min read SQL SUM() Function 5 min read SQL MAX() Function 4 min read AVG() Function in SQL 4 min read Advanced SQL TopicsSQL Subquery 4 min read Window Functions in SQL 6 min read SQL Stored Procedures 7 min read SQL Triggers 5 min read SQL Performance Tuning 6 min read SQL TRANSACTIONS 6 min read Database Design & SecurityIntroduction of ER Model 10 min read Introduction to Database Normalization 6 min read SQL Injection 11 min read SQL Data Encryption 5 min read SQL Backup 4 min read What is Object-Relational Mapping (ORM) in DBMS? 7 min read Like