C program to count number of white spaces in string: Count Number of white spaces in the string is a simple string-based program.

White spaces in c
In c programming, white spaces is any character series that represents space horizontally or vertically. In ASCII, white space characters are space ( ‘ ‘ ),tab ( ‘\t’ ), carriage return ( ‘\r’ ), newline ( ‘\n’ ), vertical tab ( ‘\v’ ) and form-feed ( ‘\f’ ).

Example :
String = white space program
White spaces in string = 2

Algorithm to count white spaces in string

  1. Declare variables
  2. Set counter = 0
  3. Take input from the user. str
  4. Traverse string str from start to end
    if str[character] == space
    counter++
  5. Print the count for white spaces. counter

C program to count number of white spaces in string

#include <stdio.h>

void main()
{
char str [81];
int i, nspace;
printf("Enter a line of text:\n");
gets(str);
i = 0;
while (str[i] != '
#include <stdio.h>

void main()
{
char str [81];
int i, nspace;
printf("Enter a line of text:\n");
gets(str);
i = 0;
while (str[i] != '\0')
{
char ch= str[i];
if (ch == ' ' || ch =='\n' || ch == '\t')
nspace++;
i++;
}
printf("White spaces in string: %d", nspace);
}
') { char ch= str[i]; if (ch == ' ' || ch =='\n' || ch == '\t') nspace++; i++; } printf("White spaces in string: %d", nspace); }

Output :

C program to count number of white spaces in string

Write A Comment