String

Count Number of space of the given string in C language

Count Number of space of the given string in C language

In this article, we will discuss the concept of the Count Number of space of the given string in C language

In this post, we are going to learn how to  count the number of spaces in  the given string in C programming language

When the above code is executed, it produces the following result

Enter a string
C program to count space of a string
Total white space:7


C code to count space of the given string using while loop

The program allows the user to enter a String and then it counts the number of white spaces in the given string using while loop in C programing language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
char str[100];
int i;
int space=0;
    printf("Enter a string\n");

    gets(str);
    i=0;
    while(i<=str[i]){
 if(str[i]==' '){
        space++;
    }
    i++;
}

printf("Total white space :%d ",space);
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string
space counter code in C language
Total white space:5

 

C code to count space of the given string using do-while loop

The program allows the user to enter a String and then it counts the number of white space in the given string using do-while loop in C programing language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
char str[100];
int i;
int space=0;
    printf("Enter a string\n");

    gets(str);
    i=0;
    do{
 if(str[i]==' '){
        space++;
    }
    i++;
}while(i<=str[i]);

printf("Total white space :%d ",space);
//getch();
    return 0;
}


When the above code is executed, it produces the following result

Enter a string
C program for count space
Total white space:4

 

Approaches

  1. Declare a Caracter array as char ch[100];
  2. Declare and initialize integer variables as space=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A loop(for, while and do-while) is used to count space in the given string.
  6. It is initialized as i=0, checks the condition whether i<=str[i]; and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str[i]==’  ‘), when the if statements is true,  The space becomes space + 1( space = space +1);
  8. When it is false, terminates the loop
  9. Finally, the program displays the total number of space of the given string

 

 

Suggested for you

for loop in C language

while loop in C language

do-while loop in C language

 

Similar post

C++ program to find the total number of characters in the given string

C program to find the total number of characters in the given string

Python program to find the total number of characters in the given string

 

Java program to count the total number of vowels and consonants digits special characters and spaces

C program to count the total number of vowels and consonants digits special characters and spaces

C++ program to count the total number of vowels and consonants digits special characters and spaces

 

Program to count white space of the given string in Java
C++ code: Count Number of space of the given string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago