C program to find the number of lines, words and characters in a string:
In this post, we will learn how to find the total number of lines or newlines, words and characters in a user given string using C programming language. We will write one program that will take one string as input from the user, finds the total count of each and print them on the console.
With this program, you will learn:
- How to read user inputs in C
- How to read strings with blank spaces in C
- How to iterate through the characters of a string in C
- How to print data on console in C.
Reading and storing a string in C:
Strings are sequence of characters. Each string is an array of characters. There is not any datatype available specifically for strings in C. So, we need to use a character array to hold a string.
We can use scanf to read an user-input string and printf to print a string to the console. For example:
#include <stdio.h>
int main()
{
char input[100];
scanf("%s", input);
printf("%s\n", input);
return 0;
}
It will work for words, i.e. if your string has multiple words, it will read only the first one.
In this example, it reads only hello.
If you want to read a string with blank spaces, you have to use scanf as like below:
#include <stdio.h>
int main()
{
char input[100];
scanf("%[^\n]", input);
printf("%s\n", input);
return 0;
}
It will read strings with blank spaces.
But, we have to read strings with new lines. The above example will stop taking input if it finds a newline. So, we can’t enter any multiline string.
For that, we can define the end character, i.e. scanf will read until it finds that character.
#include <stdio.h>
int main()
{
char input[100];
scanf("%[^|]", input);
for(int i = 0; input[i] != '\0'; i++){
printf("%c\n", input[i]);
}
return 0;
}
In this example, scanf will read until it find |. So, we can enter multiline strings and it will work.
Algorithm to count the number of lines, words and characters in a string:
The end character is a null character, \0 of a C string. We will use a loop to iterate through the characters of the string one by one. For each character, we will check the type and increment the value of word, line or character.
- Initialize three variables to hold the word, lines and character count and initialize these as 0.
- If the character is a blank space, increment the word counter by 1.
- If the character is a newline, increment the word and lines counter by 1.
- If the character is something else, increment the character count by 1.
- If the character is \0, increment the word and lines counter by 1 if total character is not zero. This check will ensure that for empty strings, we are not increasing the word and lines counter. But for non-empty strings, we have to increase the words and lines count by 1 as the last character is \0 and we don’t have any blank space or newline before it.
C program:
Below is the complete C program:
#include <stdio.h>
int main()
{
int words = 0, characters = 0, lines = 0;
char input[100];
input[0] = '\0';
char prevChar = ' ';
printf("Enter a string:\n");
scanf("%[^|]", input);
printf("%s", input);
if (input[0] != '\0')
{
for (int i = 0; input[i] != '\0'; i++)
{
if (input[i] == ' ' && prevChar != ' ' && prevChar != '\n')
{
words++;
}
else if (input[i] == '\n')
{
lines++;
if (prevChar != ' ' && prevChar != '\n')
words++;
}
else if (input[i] != ' ' && input[i] != '\n')
{
characters++;
}
prevChar = input[i];
}
if (characters > 0 && prevChar != ' ' && prevChar != '\n')
{
words++;
lines++;
}
}
printf("Words: %d\nCharacters: %d\nLines: %d\n", words, characters, lines);
return 0;
}
We are following the same steps we discussed before. The only difference is that we are also checking the previous character on each step.
- Store the character in the prevChar variable
- On each step, check the prevChar variable before increment any value. For example, for the string Hello World, it will ignore all extra whitespaces and print 2 as the total number of words.
Sample output:
If you run this program, it will print output as like below:
Enter a string:
hello world
hello universe|
Words: 4
Characters: 23
Lines: 2
Enter a string:
hello world
hello universe
|
Words: 4
Characters: 23
Lines: 5
Enter a string:
|
Words: 0
Characters: 0
Lines: 1
Enter a string:
|
Words: 0
Characters: 0
Lines: 0
You might also like:
- C program to multiply two numbers using plus or addition operator
- C program to print the name using array
- C program to save the output of a program to file
- C program to store and display the details of an employee by using structure
- C program to find the average runs of cricket players using structure
- C program to convert a string to hexadecimal value
- C program to swap two bits of a number