#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int count_letters(string);
int count_words(string);
int count_sentences(string);
float l, w, s;
int main(void)
{
//Get text
string text = get_string("Text:");
//Get the length of text
int i = strlen(text);
//Coleman-Liau index
float L = 100.0f * (l / (float)w);
float S = 100.0f * (s / (float)w);
int index = round(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 %i\n", index);
}
}
int count_letters(string text)
{
//Letters start from 0
l = 0;
for (int i = 0, n = strlen(text); i < n; i )
{
if (isalpha(text[i]))
{
l ;
}
}
return l;
}
int count_words(string text)
{
//Spaces 1 equals to count of words
w = 1;
for (int i = 0, n = strlen(text); i < n; i )
{
//Space equals to number 32 in ASCII Chart
if(text[i] == ' ')
{
w ;
}
}
return w;
}
int count_sentences(string text)
{
s = 0;
for (int i = 0, n = strlen(text); i < n; i )
{
if(text[i] == '!' || text[i] == '.' || text[i] == '?')
{
s ;
}
}
return s;
}
The index is always the same negative number so I can only get "Before Grade 1" output because i don't use any of my created functions.
I have created 3 functions below my code but I don't know how to use them in main.I want my functions to be able to calculate the count of letters(l), words(w) and sentences(s) in a text in order to find the index.Can you help me?
CodePudding user response:
Just create a variable and then call the function. The value returned by that function (hopefully an integer in this case) would be stored in it. Just as an example,
int l = count_letters(text);
Do the same thing for the other 2 functions and you are done. Also define this before Coleman-Liau index expressions.
Also remove those global variables (on line 11 probably) you don't need those.