# C Language: Calculating the Level of Difficulty of a Text Using the Coleman-Liau Index

We continue to take the Computer Science CS50 (Harvard) course, and this time we will do the task of calculating the level of complexity of the text according to the Colman-Liau index (readability.c) in the C programming language. According to the idea of the two authors of the method, we can estimate the complexity of the text and thus distribute books by school grades (from simple texts, where the index is less than 1, to academically complex ones with an index greater than 16). The calculation is based on the formula:

`index = 0.0588 * L - 0.296 * S - 15.8` ,
where L is the average number of letters per 100 words in the text,
S is the average number of sentences per 100 words in the text.

Coding algorithm:

1. Accept text input;
2. Count the number of letters, words and sentences in a text;
3. Calculate the Coleman-Liau index and round the resulting value;
4. If the index is less than 1, then write “Before Grade 1”, if the index is greater than 16 – “Grade 16+”, in other cases, just print the Grade number.

Igroglaz’s solution. As usual, I do not write using the cs50.h library, while Shtukentsia uses it in his example below.

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

int count_letters (char text);
int main(void)
{
char text[1000];
int letters = 0;
int words = 0;
int sentances = 0;

printf("Text: \n");

// scanf("%s", text); won't work as input as it stops upon ' ' (space)

for (int i = 0; ; i++)
{
scanf("%c", &text[i]);
if ((text[i] >= 'A' && text[i] <= 'Z') ||
(text[i] >= 'a' && text[i] <= 'z'))
letters++;
if (text[i] == ' ')
words++;
switch (text[i])
{
case ('.'): sentances++; break;
case ('!'): sentances++; break;
case ('?'): sentances++; break;
}
if (text[i] == '\n') break;
}

words++; // words +1 cause of last word which doesn't have ' ' space

printf("Letters: %d\n"
"Words: %d\n"
"Sentances: %d\n", letters, words, sentances);

float L = (letters * 100.0) / words; // avg letters per 100 words
float S = (sentances * 100.0) / words; // avg sentances per 100 words
float grade = (0.0588 * L - 0.296 * S - 15.8); // Coleman-Liau index

if (grade < 1)
printf("Before Grade 1");
else if (grade > 15)
printf("Grade 16+");
else
printf("Grade %.0f", round(grade));

getchar();
getchar();
return 0;
}
```

Solution (by Shtukentsia) with cs50 library:

```#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int count_letters(string text);
int count_words(string text);
int count_sentance(string text);

int main(void)
{
string text = get_string("Your text:");
int sum_letters = count_letters(text);
int sum_words = count_words(text);
int sum_sentance = count_sentance(text);
printf("Sum of lettels:%d\n",sum_letters);
printf("Sum of words:%d\n",sum_words);
printf("Sum of sentances:%d\n",sum_sentance);
float L = (100.0*sum_letters)/sum_words;
float S = (100.0*sum_sentance)/sum_words;
float index = 0.0588 * L - 0.296 * S - 15.8;

if (index<1)
{
printf("Before Grade 1\n");
}
else if (index>16)
{
printf("Grade 16+\n");
}
else

printf("Grade %d\n",(int)rint(index));
}

int count_letters(string text)
{
printf("%s\n", text);
int i=0;
int text_lenth=strlen(text);
int sum_letters=0;

for(i=0; i<=text_lenth; i++)
{
if (isalpha(text[i]))
sum_letters+=1;
}

return sum_letters;
}

int count_words(string text)
{
int i=0;
int text_lenth=strlen(text);
int sum_words=1;

for(i=0; i<=text_lenth; i++)
{
if (isspace(text[i]))
sum_words+=1;
}

return sum_words;
}

int count_sentance(string text)
{
int i=0;
int text_lenth=strlen(text);
int sum_sentance=0;

for(i=0; i<=text_lenth; i++)
{
char sent=text[i];
if (sent == '.' || sent == '!' || sent == '?')
sum_sentance+=1;
}
return sum_sentance;
}```

Running the program and testing on the example text:
“One fish. Two fish. Red fish. Blue fish.”

After final checking bugs and style, my code looks like this:

```#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int count_letters(string text);
int count_words(string text);
int count_sentance(string text);

// Check level of text by complexity

int main(void)
{
string text = get_string("Your text:");
int sum_letters = count_letters(text);
int sum_words = count_words(text);
int sum_sentance = count_sentance(text);
printf("Sum of lettels:%d\n", sum_letters);
printf("Sum of words:%d\n", sum_words);
printf("Sum of sentances:%d\n", sum_sentance);
float L = (100.0 * sum_letters) / sum_words;
float S = (100.0 * sum_sentance) / sum_words;
float index = 0.0588 * L - 0.296 * S - 15.8; // Formula of Colman-Liau index

if (index < 1)
{
printf("Before Grade 1\n");
}

else if (index > 16)
{
printf("Grade 16+\n");
}

else
{
printf("Grade %d\n", (int)rint(index));
}

}

// Count letters in text

int count_letters(string text)
{
printf("%s\n", text);
int i = 0;
int text_lenth = strlen(text);
int sum_letters = 0;

for (i = 0; i <= text_lenth; i++)
{
if (isalpha(text[i])) // Check if this character is alphabetical
{
sum_letters += 1;
}
}

return sum_letters;
}

// Count words in text

int count_words(string text)
{
int i = 0;
int text_lenth = strlen(text);
int sum_words = 1;

for (i = 0; i <= text_lenth; i++)
{
if (isspace(text[i])) // If there is space, so next word started
{
sum_words += 1;
}

}

return sum_words;
}

// Count sentances in text

int count_sentance(string text)
{
int i = 0;
int text_lenth = strlen(text); // Check lenth of string
int sum_sentance = 0;

for (i = 0; i <= text_lenth; i++)
{
char sent = text[i];
if (sent == '.' || sent == '!' || sent == '?') // Checking the number of sentences by punctuation marks
{
sum_sentance += 1;
}

}
return sum_sentance;
}```

This entry was posted in C language (en). Bookmark the permalink.