# C program to count the number of lines, words and characters

Jan. 29, 2021 39084

Today, we are going discuss about one of the most common string questions asked in interviews and coding rounds of many companies. Most of the time this question is often asked in sub-parts, but today we will encounter all the subparts of this question.

There is null character ('\0')  at the end of the string. It has a numeric value of zero and that can be used to represent the end of a string of characters, such as a word or phrase. This helps us to determine the length of strings.

Coming back to the problem, this problem wants us to count the number of lines, words and characters inside a string given by the user.

### Example Case

Input : Anand Sagar
Volleyball player
Output: Total number of words : 4
Total number of lines : 2
Total number of characters :  26

So, in the given example input above, there are 4 words, 2 lines and 26 characters

## Approach

​In order to solve this problem, we are going to traverse the entire string. And take one counter each for count of words, new line and characters.  We will print all the counter variables respectively once we reach the null character of the string.

We will increase words counter every time we encounter a space or a new line. We will increase new line counter every time we encounter a new line input ( ' \n '), and rest for all the cases we will increase the counter for characters.

Following  are some corner cases to be taken care of in the program :

1. We have to increment word count every time there is new line, because a new line starts with a new word.
2. There is no space for the last word of the string, so, if there are more than 1 characters inside the string, we should increment words counter by 1 at the end.
3. If the number of characters are more than 1, then we should increment new line counter by 1 because the string starts from line 1 and we have count that too since there is no new line input for the first line.

## Algorithm

1. Take a string as input and store it in the array of characters.
2. Create 3 counter variable for the count of words, lines and characters in the string.
3. Using for loop search for a space ' ' in the string and consecutively increment the  variable count for words.
4. Using for loop search for a next line '\n' in the string and consecutively increment a variable count for next line.
5. Using for loop search for a characters except space and new line in the string and consecutively increment a variable count for characters.
6. Repeat step 3,4,5 until the loop reaches to the end of the string.
7. Check for the corner cases ( discussed above) and do accordingly.
8. Print all the values of the counter.

## Code

We are going to take character array of size 100 for input. It is up to the programmer and the need of the problem.

A thought must have gazed in your mind that how can we be so sure that the input will be given exactly 100 characters, so yes we are not sure of that and to deal with that we are going to use
scanf with formatting. We will scan characters like this scanf("%[^~]",str[i]). Let me explain its advantages and usage.
So,  ' ^ ' this is a caret operator. It helps to ignore a particular operator. So, now our scanf will take input until the user presses '~'.
For example, the scanf[^\n] takes all characters till a newline character is encountered. So, let input string is “Hello World”. scanf(“%s”,&str) will store “Hello” in str since by default str terminates at space. scanf(“%[^\n]”,&str) will store “Hello World” in str since now string input is captured till new line is encountered.
So, this way the user can give the input according to the size he wants and when the user is done, we can ask him to enter '~' after input completion.​

//C code
#include <stdio.h>
int main()
{
char str[100];//input string with size 100

int words=0,newline=0,characters=0; // counter variables

scanf("%[^~]",&str);//scanf formatting

for(int i=0;str[i]!='\0';i++)
{
if(str[i] == ' ')
{
words++;
}
else if(str[i] == '\n')
{
newline++;
words++;//since with every next line new words start. corner case 1
}
else if(str[i] != ' ' && str[i] != '\n'){
characters++;
}
}
if(characters > 0)//Corner case 2,3.
{
words++;
newline++;
}
printf("Total number of words : %d\n",words);
printf("Total number of lines : %d\n",newline);
printf("Total number of characters : %d\n",characters);
return 0;
}


Input : Deepak Kumar is the
Output : Total number of words : 8
Total number of lines : 2
Total number of characters : 36

## Complexity Analysis

As we iterated till the end of the string the time complexity of this approach will be $O ( n )$  where n is the length of the string and the space complexity of this program will be $O(1)$.

There are many variations of this problem, and there are few more corner cases where the interviewer may ask you to do changes, but the most important thing is to understand the approach of solving the question, so that it will not be difficult for you make small modifications asked by your interviewer.

Liked the post?
Hi, I am Priyansh Jha. I am a pre-final year student of Electronics and Communication major undergraduate.
Editor's Picks
0 COMMENT