In this tutorial you will learn about the C Program to count Characters with and without Space and its application with practical example.
C Program to count Characters with and without Space
In this tutorial, you will learn about the C Program to count Characters with and without Space with a practical example.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Concepts of while loop.
- Conditional Statements in C programming.
- Using file functions of c language.
Count Characters with and without Space
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a while loop for counting. The C programming language has many pre-defined functions for string manipulation. but in today’s tutorial, we will count Characters with and without Space.
With the help of this program, we can count Characters with and without Space
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input string from the user. 3. Reading the string. 4. Counting the Data in the string. 5. Printing the results. 6. End program. |
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include<stdio.h> int main() { //declaraiton of variables in program char str[100]; int i, countWithSpace=0, countWithoutSpace=0; //Taking the input string from the user printf("Enter any string: "); //reading the string from the user gets(str); //reading characters from taken input string for(i=0; str[i]!='\0'; i++) { countWithSpace++; } for(i=0; i<countWithSpace; i++) { if(str[i]==32) countWithoutSpace++; } //printing the number of characters with and without spaces. countWithoutSpace = countWithSpace-countWithoutSpace; printf("\nNumber of character (with space) = %d", countWithSpace); printf("\nNumber of character (without space) = %d", countWithoutSpace); return 0; } |
Output:-
In the above program, we have first initialized the required variable.

- str[100] = it will hold the string value.
- i= it will hold the integer value of the loop.
- countWithSpace = it will hold the integer value of the counting.
- countWithoutSpace= it will hold the integer value of the counting.
Taking Input string from the user.

Counting the characters from the string entered by the user .
Printing output data characters.