C program: Count word,characters and space of a string
C program: Count word, characters and space of a string
In this article, we will discuss the concept of the C program: Count word, characters and space of a string
In this post, we are going to learn how to count the total number of words, character and Space of the given string in C programming language
C program: Count word, characters and space of a string
Count words, character and Space using for loop
The program allows the user to enter a String and then it counts and display the total number of words, character and Space of the given string using for loop in C programing language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { char str[100];//declare and initialize char array int i; int words=1,characters=0,space=0;//display and initiolize variables printf("Please enter the string \n"); gets(str);//stored the string entered by user for(i=0; str[i] != '\0'; i++){ if(str[i]!=' '){ // check characters characters++; } else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ //check words words++; } } printf("\nTotal words: %d ",words); //display words printf("\nTotal characters: %d",characters);//display characters printf("\nSpace: %d ",(words-1));//display space getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string This is C language Total words: 4 Total characters: 15 Space: 3
Approach
- Declare a Character array as char str[100];
- Declare and initialize integer variables as words=1,space=0,caracters=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A for loop is used to count total characters of the given string.
- It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str[i]!=’ ‘), if it is true, The characters becomes characters + 1( characters = characters +1);
- When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The words becomes words+1(words=words+1);
- Finally, the program displays the total number of the words, characters and Space of the given string
Count word, characters and space using while loop
The program allows the user to enter a String and then it counts and display the total number of words, character and Space of 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 words=1,characters=0,space=0; printf("Please enter the string \n"); gets(str); //store the given string i=0; while(str[i] != '\0'){ if(str[i]!=' '){ // count characters characters++; } else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ words++; // count words } i++; } printf("\nTotal words: %d ",words); //display total number of words printf("\nTotal characters: %d",characters); //display total number of characters printf("\nSpace: %d ",(words-1)); //display total number of space getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string code4coding.com C language tutorials Total words: 4 Total characters: 33 Space: 3
Approach
- Declare a Character array as char str[100];
- Declare and initialize integer variables as words=1,space=0,caracters=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A while loop is used to count total characters of the given string.
- It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str[i]!=’ ‘), if it is true, The characters becomes characters + 1( characters = characters +1);
- When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The words becomes words+1(words=words+1);
- Finally, the program displays the total number of the words, characters and Space of the given string
Count word, characters and space using do-while loop
The program allows the user to enter a String and then it counts and display the total number of words, character and Space of 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 words=1,characters=0,space=0; printf("Please enter the string \n"); gets(str); //count characters of a string wit out space i=0; do{ if(str[i]!=' '){ // checl characters characters++; } else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t'){ words++; //check words } i++; }while(str[i] != '\0'); printf("\nTotal words: %d ",words); //display total number of words printf("\nTotal characters: %d",characters); //display total number of characters printf("\nSpace: %d ",(words-1)); //display total number of space getch(); return 0; }
When the above code is executed, it produces the following result
Please enter the string Learn C basic tutorials code for coding Total words: 7 Total characters: 33 Space: 6
Approach
- Declare a Character array as char str[100];
- Declare and initialize integer variables as words=1,space=0,caracters=0;
- The user asked to enter a string
- The given string is stored in the variable str;
- A do-while loop is used to count total characters of the given string.
- It is initialized as i=0, checks the condition whether str[i] != ‘\0’; and executes the loop until the given condition becomes true
- Use an if condition to test if(str[i]!=’ ‘), if it is true, The characters becomes characters + 1( characters = characters +1);
- When it is false, control moves to the else-if part and evaluates the test-expression of else-if. if it is true The words becomes words+1(words=words+1);
- Finally, the program displays the total number of the words, characters and Space of the given string
Suggested for you
Similar post
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character
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