Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- int count_letters(string text);
- int count_words(string text);
- int count_sentences(string text);
- int main(void)
- {
- string text = get_string("Text: ");
- //printf("%i \n %i \n %i \n", count_letters(text), count_words(text), count_sentences(text));
- float num_letters = (float) count_letters(text);
- float num_words = (float) count_words(text);
- float num_sentences = (float) count_sentences(text);
- //index = 0.0588 * L - 0.296 * S - 15.8
- //L is the avg of letters per 100 words
- float L = (num_letters / num_words) * 100.0;
- //S is the avg of sentences per 100 words.
- float S = (num_sentences / num_words) * 100.0;
- int index = (int) round(0.0588 * L - 0.296 * S - 15.8);
- //printf("%f %f %f\n", num_letters, num_words, num_sentences);
- //printf("%f %f %i\n", L, S, (int) round(I));
- if (index > 1 && index < 16)
- {
- printf("Grade %i\n", index);
- }
- else if (index < 1)
- {
- printf("Before Grade 1\n");
- }
- else
- {
- printf("Grade 16+\n");
- }
- }
- int count_letters(string text)
- {
- int count = 0;
- for (int i = 0, length = strlen(text); i < length; i++)
- {
- int number = (int) text[i];
- if ((number >= 97 && number <= 122) || (number >= 65 && number <= 90))
- {
- count ++;
- }
- }
- return count;
- }
- int count_words(string text)
- {
- int count = 0;
- for (int i = 0, length = strlen(text); i < length; i++)
- {
- //if at the last character end loop
- if (i == length - 1)
- {
- //current char
- int number = (int) text[i];
- int prev_number = (int) text[i - 1];
- if ((number > 65 && number < 90) || (number > 97 && number < 122))
- {
- count ++;
- }
- else if (prev_number != 32)
- {
- count ++;
- }
- }
- else
- {
- //current char
- int number = (int) text[i];
- //next char
- int next_number = (int) text[i + 1];
- //if the next char is a space
- if (next_number == 32)
- {
- //check if current char is not a space
- if (number != 32)
- {
- count ++;
- }
- }
- }
- }
- return count;
- }
- int count_sentences(string text)
- {
- int count = 0;
- for (int i = 0, length = strlen(text); i < length; i++)
- {
- int number = (int) text[i];
- if (number == 33 || number == 63 || number == 46)
- {
- count++;
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement