Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Again I have to say I do not code in C and I learnt the syntax by how you coded, so there might be places where it wouldn't compile as I wasn't coding alongside with an IDE nor do I have sufficient datasets and modules given in your IDE so I would not be able to debug.
- Treat this as pseudocode.
- Have fun messing around and learning more about style and optimisation.
- Chloe x.
- */
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include<ctype.h>
- // defining functions
- int count_letters(char* str); // char* is basically string
- int count_words(char* str1);
- int count_sentences(char* str2);
- int calculate_index(int s, int w, int l);
- // the main function
- int main(void)
- {
- char* text = get_string("Text: ");
- int num_letters = count_letters(text);
- int num_words = count_words(text);
- int num_sentences = count_sentences(text);
- int val_index = calculate_index(num_sentences, num_words, num_letters);
- printf("%i letters(s)\n", num_letters);
- printf("%i words(s)\n", num_words);
- printf("%i sentence(s)\n", num_sentences);
- if (val_index<1)
- {
- printf("Before Grade 1\n");
- }
- else if (val_index>16)
- {
- printf("Grade 16+\n");
- }
- else
- {
- printf("Grade %i\n", val_index);
- }
- return 0;
- }
- // counting letters
- int count_letters(char* str)
- {
- int num_letters=0;
- for (int i=0; i<strlen(str); i++)
- {
- if isalnum(str[i])
- {
- num_letters++; // you used a = a + 1, which has a shorthand of a++ which is very useful
- }
- }
- return num_letters;
- }
- // counting words
- int count_words (char* str)
- {
- int num_words=0;
- for (int i=0; i<strlen(str); i++)
- {
- if isblank(str[i])
- {
- num_words++; // same as above, changed to ++
- }
- }
- if (str != 0)
- // added the check null function and in your own code you reinitialised number of words which would always return 1
- {
- num_words++; // same as above, changed to ++
- }
- return num_words;
- }
- // counting sentences
- int count_sentences (char* str)
- {
- int num_sentences=0;
- for (int i=0; i<strlen(str);i++)
- {
- if (str[i]=='!'||str[i]=='.'||str[i]=='?')
- {
- num_sentences++; // same as above, changed to ++
- }
- }
- return num_sentences;
- }
- //calculating index
- int calculate_index (int s, int w, int l)
- // I made it s w l to make it way more readable and line length doesn't get too long
- {
- int L = l/w*100; // this would produce unnecessary rounding as type is int so it would be rounded down
- int S = s/w*100; // same issue
- int CLindex= 0.0588 * L - 0.296 * S - 15.8;
- return CLindex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement