---
title: "COBOL INSPECT Useful Logic to Find Spaces in String"
description: "Inspect in COBOL is a feature in COBOL to handle strings. You can manipulate strings using INSPECT verb."
url: https://srinimf.com/2016/06/28/how-to-find-number-of-spaces-in-a-string-using-inspect/
date: 2016-06-28
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2016/06/pexels-photo-3825540.jpeg?fit=1820%2C1300&ssl=1
categories: ["COBOL"]
tags: ["Inspect COBOL"]
type: post
lang: en
---

# COBOL INSPECT Useful Logic to Find Spaces in String

The below logic helps you to find spaces in a string using INSPECT statement in COBOL. The [sample logic](https://srinimf.com/2017/03/21/how-to-read-indexed-file-syntax/) tells [how many](https://srinimf.com/2016/06/27/how-many-sysdummy-tables-are-present-in-db2/) spaces are present in a string.

Related[COBOL INSPECT these four top examples you need to try](https://srinimf.com/2012/06/03/cobol-inspect-verb-4-benefits/)

## Logic with INSPECT to find Spaces in String

IDENTIFICATION DIVISION.

PROGRAM-ID. HELLO.

Related[How to write COBOL Hello World program in simple way](https://srinimf.com/2016/08/22/how-to-write-cobol-hello-world-program-in-simple-way/)

[DATA DIVISION](https://srinimf.com/2015/06/23/cobol-data-division-clauses-1-of-3/).
WORKING-STORAGE SECTION.
01 WS-CNT1 PIC 9(2) VALUE 0.
01 WS-CNT2 PIC 9(2) VALUE 0.
01 WS-STRING PIC X(15) VALUE ‘ABCDACD””ADEAAAFF’.

PROCEDURE DIVISION.
INSPECT WS-STRING TALLYING WS-CNT1 FOR ALL CHARACTERS.
DISPLAY “WS-CNT1 : “WS-CNT1.
INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL SPACES
DISPLAY “WS-CNT2 : “WS-CNT2

/* The WS_CNT2 value now is ‘2’. Since there are 2 spaces in input string*/
STOP RUN.

Related posts

- [COBOL Strings, Here are Ideas (1 of 2)](https://srinimf.com/2010/04/07/mainframecobol-strings-inspectconcatenation-splitting1of2/)DateApril 7, 2010
- [COBOL really you need ideas to modify strings](https://srinimf.com/2016/06/01/cobol-examples-for-inspect-replacing-and-converting/)DateJune 1, 2016
- [COBOL INSPECT these four top examples you need to try](https://srinimf.com/2012/06/03/cobol-inspect-verb-4-benefits/)DateJune 3, 2012

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2016%2F06%2F28%2Fhow-to-find-number-of-spaces-in-a-string-using-inspect%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2016%2F06%2Fpexels-photo-3825540.jpeg%3Ffit%3D1200%252C857%26ssl%3D1&description=COBOL%20INSPECT%20Useful%20Logic%20to%20Find%20Spaces%20in%20String)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2016/06/28/how-to-find-number-of-spaces-in-a-string-using-inspect/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2016/06/28/how-to-find-number-of-spaces-in-a-string-using-inspect/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2016/06/28/how-to-find-number-of-spaces-in-a-string-using-inspect/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2016/06/28/how-to-find-number-of-spaces-in-a-string-using-inspect/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20COBOL%20INSPECT%20Useful%20Logic%20to%20Find%20Spaces%20in%20String&body=https%3A%2F%2Fsrinimf.com%2F2016%2F06%2F28%2Fhow-to-find-number-of-spaces-in-a-string-using-inspect%2F&share=email)
-

### Like this:

Like Loading…
