C Program to Count the Characters in the String Except Space


This article will guide you through a C program to count the characters in the string except space.

Also Read: 25 Interview Questions On C: A Comprehensive Guide for Success

By following the steps outlined below, you can enhance your programming skills and efficiently solve this problem.

In the programming realm, manipulating characters is a common task. One such task involves counting the characters in a string while excluding spaces.

Also Read: Array of Pointers in C: A Comprehensive Guide

Let’s dive in!

C Program to Count Characters in the String Except Space

To begin, let’s understand the approach we’ll take to solve this problem.

Also Read: Static Functions in C: A Complete Guide

The C program we’ll develop will iterate through each character in the given string, check if it’s a space, and increment the count if it isn’t.

This way, we’ll obtain the count of characters in the string, excluding spaces.

Here’s the C program:

#include <stdio.h>

int main() {
    char str[100];
    int count = 0;
    printf("Enter a string: ");
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] != ' ') {
    printf("Count of characters in the string except space: %d\n", count);
    return 0;

Also Read: Also Read: Print Contents of File in Reverse Order in C

Let’s break down the program step by step for a better understanding.

  1. We include the necessary header file, stdio.h, which provides input/output functions in C.
  2. Inside the main() function, we declare a character array str with a maximum size of 100 characters. This array will store the user input.
  3. We initialize a variable count to track the number of characters in the string, excluding spaces.
  4. Using the printf() function, we prompt the user to enter a string.
  5. Next, we read the user input using the gets() function and store it in the str array.
  6. Now, we enter a for loop that iterates through each character of the string until the null character '\0' is encountered, indicating the end of the string.
  7. Inside the loop, we check if the current character is not a space ' '. If it isn’t, we increment the count variable.
  8. Once the loop completes, we use the printf() function to display the final count of characters in the string, excluding spaces.
  9. Finally, we return 0 to indicate successful program execution.

Now that we have our C program ready, let’s move on to some frequently asked questions related to counting characters in a string, excluding spaces.

Also Read: C Program to Find the Sum of Cubes of Elements in an Array

FAQs: Counting Characters in a String Except Spaces

1. What happens if the user enters an empty string?

In the case of an empty string, i.e., a string without any characters, the program still considers it as one character (the null character '\0'). Hence, the count will be 1.

2. Can I count the characters in a string including spaces?

To count the characters in a string, including spaces, simply remove the condition str[i] != ' ' from the program. This way, every character encountered in the string will be counted, regardless of whether it’s a space or not.

3. How can I handle leading and trailing spaces in the string?

To handle leading and trailing spaces, modify the program to skip counting spaces at the beginning and end of the string. One approach is to use two separate loops—one to skip leading spaces and another to count the characters.

4. Is it possible to count characters in multiple strings using the same program?

No, the provided program counts characters in a single string at a time. If you want to count characters in multiple strings, you can incorporate the program into a loop or write a separate function that accepts a string as a parameter.

5. Can I count characters in a string using other programming languages?

Certainly! You can count characters in a string using other programming languages as well. The logic will be similar, although the syntax may differ. For example, in Python, you can use the len() function to find the length of a string and then subtract the count of spaces using the str.count() method.

6. How efficient is this program for large strings?

The provided C program has a linear time complexity of O(n), where n is the length of the string. It traverses the entire string once, incrementing the count for each non-space character encountered. Thus, it performs well even for large strings.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered


In this article, we explored a C program that efficiently counts the characters in a given string while excluding spaces.

Also Read: Search a Character in a String Using a Pointer in C

We learned the step-by-step implementation and also addressed some common questions related to this topic.

By practicing and understanding such programming concepts, you can enhance your problem-solving skills and become a proficient programmer.