Codeforcoding

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

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

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

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

C program to count the total number of characters

Code to count the total number of characters using for loop

The program allows the user to enter a string(or sentence) and thereafter It counts the total characters of the string without space using for loop in C language

Program 1

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

int main()
{
    char str[100];//character array
    int i,totChar;//variable  declaration
totChar=0;//variable initialization
    printf("Please enter the string for count words\n");
    gets(str);//get and store string from user

    for(i=0; str[i] != '\0'; i++){
        if(str[i]!=' ')// this condition is used to avoid counting space
        {
            totChar++;//totChar=totChar+1
        }
    }
    printf("The total characters of the given string= %d",totChar);
    getch();//display total characters of the string
    return 0;
}

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

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

 

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 using the totChar variable
  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)!=’ ‘), 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 using while loop

The program allows the user to enter a string and thereafter It counts the total characters of the string without space using while  loop in C 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);//store the string 
//count characters of a string wit out space
i=0;
    while(str[i] != '\0'){
        if(str[i]!=' ')// this condition is used to avoid counting space
        {
            totChar++;
        }
         i++;
    }
    printf("The total characters of the given string= %d",totChar);
    getch();
    return 0;
}

When the above 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= 20

 

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. Initialized an integer variable as i=0;
  5. A while-loop is used to count total characters of the given string using the totChar variable
  6. The while-loop checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  totChar  becomes totChar + 1(totChar=totChar+1)
  8. Finally, the program displays the total number of the character using the totChar variable

 

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

The program allows the user to enter a string and thereafter It counts the total characters of the string without space using do-while loop in C language

Program 3

#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);//takes and store te string from the user
//count characters of a string wit out space
i=0;
    do{
        if(str[i]!=' ')// this condition is used to avoid counting space
        {
            totChar++;
        }
         i++;
    }while(str[i] != '\0');
    printf("The total characters of the given string= %d",totChar);
    getch();
    return 0;
}

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

Please enter the string for count characters
C is an easy language
The total characters of the given string= 17

 

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. Initialized an integer variable as i=0;
  5. A do-while-loop is used to count total characters of the given string using the totChar variable
  6. The do-while-loop checks the condition whether str[i] != ‘\0’;  and executes the loop until the given condition becomes true
  7. Use an if condition to test if(str.charAt(i)!=’ ‘), if it is true,  totChar  becomes totChar + 1(totChar=totChar+1)
  8. 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

Java 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
C++ program to count the total number of characters in the given string
Exit mobile version