Alphabet

C program to Count the total number of characters in the given string including space

C program to Count the total number of characters in the given string including space

In this article, we will discuss the concept of the C program to Count the total number of characters in the given string including space

In this post, we are going to learn how to count the total number of character including space of the given String in C programming language

Count character

Code to count the total number of characters in the given string

Code to count the total number of characters in the given string using for loop

The program allows the user to enter a String and then it counts and display the number of characters including space of the given string using for loop in C programing language

Program 1

#include <stdio.h>
#include <string.h>

int main()
{
    char str[100];
    int i,totChar;
totChar=0;
    printf("Please enter the string for count characters\n");
    gets(str);
//count characters of a string wit out space

    for(i=0; str[i] != '\0'; i++){
        if(str[i]!='\0')
        {
            totChar++;
        }
    }
    printf("The total characters of the given string= %d",totChar);
    getch();
    return 0;
}

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

Please enter the string for count characters
C programming language
The total characters of the given string= 22


Approach

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,totChar=0;
  3. The user asked to enter a string to count character
  4. A for-loop is used to count total characters of the given string
  5. It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and then executes the loop until the given condition becomes true
  6. Use an if condition to test if(str.charAt(i)!=’ \0′), if it is true,  totChar  becomes totChar + 1(totChar =totChar + 1)
  7. Finally, the program displays the total number of the character using the totChar variable

 

Code to count the total number of characters in the given string using while loop

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

Program 2

#include <stdio.h>
#include <string.h>

int main()
{
    char str[100];
    int i,totChar;
totChar=0;
    printf("Please enter the string for count characters\n");
    gets(str);
//count characters of a string wit out space
i=0;
    while(str[i] != '\0'){
        if(str[i]!='\0')
        {
            totChar++;
        }
         i++;
    }
    printf("The total characters of the given string= %d",totChar);
    getch();
    return 0;
}

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

Please enter the string for count characters
C language
The total characters of the given string= 10

 

Approach

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,totChar=0;
  3. The user asked to enter a string to count character
  4. A while-loop is used to count total characters of the given string
  5. It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and then executes the loop until the given condition becomes true
  6. Use an if condition to test if(str.charAt(i)!=’ \0′), if it is true,  totChar  becomes totChar + 1(totChar =totChar + 1)
  7. Finally, the program displays the total number of the character using the totChar variable

 

Code to count the total number of characters in the given string using do-while loop

Program 3

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

#include <stdio.h>
#include <string.h>

int main()
{
    char str[100];
    int i,totChar;
totChar=0;
    printf("Please enter the string for count characters\n");
    gets(str);
//count characters of a string wit out space
i=0;
    do{
        if(str[i]!='\0')
        {
            totChar++;
        }
         i++;
    }while(str[i] != '\0');
    printf("The total characters of the given string= %d",totChar);
    getch();
    return 0;
}

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

Please enter the string for count characters
C does not support oop concept
The total characters of the given string= 30

 

Approach

  1. Declare a character array as char str[100];
  2. Declare and initialize an integer variable as int i ,totChar=0;
  3. The user asked to enter a string to count character
  4. A do-while loop is used to count total characters of the given string.
  5. It is initialized as i=0, and checks condition whether str[i] != ‘\0’; and then executes the loop until the given condition becomes true
  6. Use an if condition to test if(str.charAt(i)!=’ \0′), if it is true,  totChar  becomes totChar + 1(totChar =totChar + 1)
  7. Finally, the program displays the total number of the character using the totChar variable

 

Suggested for you

for loop in C language

while loop in C language

do-while loop in C language

 

Similar post

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

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

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

 

Java program to count the total number of characters in the given string including space

C++ program to count the total number of characters in the given string including space

Python program to count the total number of characters in the given string including space

 

Python program to print all upper case and lower case Alphabets
Java program: count the total number of characters including space in 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago