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
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
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
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
Suggested for you
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…