Advertisement
chloelwt

sharon ho c

Apr 4th, 2020
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. /*
  2. 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.
  3. Treat this as pseudocode.
  4. Have fun messing around and learning more about style and optimisation.
  5. Chloe x.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <cs50.h>
  10. #include <string.h>
  11. #include<ctype.h>
  12.  
  13.  
  14. // defining functions
  15.  
  16. int count_letters(char* str); // char* is basically string
  17. int count_words(char* str1);
  18. int count_sentences(char* str2);
  19. int calculate_index(int s, int w, int l);
  20.  
  21.  
  22. // the main function
  23.  
  24. int main(void)
  25. {
  26.  
  27.   char* text = get_string("Text: ");
  28.   int num_letters = count_letters(text);
  29.   int num_words = count_words(text);
  30.   int num_sentences = count_sentences(text);
  31.   int val_index = calculate_index(num_sentences, num_words, num_letters);
  32.  
  33.  
  34.   printf("%i letters(s)\n", num_letters);
  35.   printf("%i words(s)\n", num_words);
  36.   printf("%i sentence(s)\n", num_sentences);
  37.  
  38.   if (val_index<1)
  39.   {
  40.     printf("Before Grade 1\n");
  41.   }
  42.   else if (val_index>16)
  43.   {
  44.     printf("Grade 16+\n");
  45.   }
  46.   else
  47.   {
  48.     printf("Grade %i\n", val_index);
  49.   }
  50.   return 0;
  51.  
  52. }
  53.  
  54. // counting letters
  55.  
  56. int count_letters(char* str)
  57. {
  58.   int num_letters=0;
  59.   for (int i=0; i<strlen(str); i++)
  60.   {
  61.     if isalnum(str[i])
  62.       {
  63.         num_letters++; // you used a = a + 1, which has a shorthand of a++ which is very useful
  64.       }
  65.   }
  66.     return num_letters;
  67. }
  68.  
  69. // counting words
  70.  
  71. int count_words (char* str)
  72. {
  73.   int num_words=0;
  74.   for (int i=0; i<strlen(str); i++)
  75.   {
  76.     if isblank(str[i])
  77.     {
  78.       num_words++; // same as above, changed to ++
  79.     }
  80.   }
  81.     if (str != 0)
  82.     // added the check null function and in your own code you reinitialised number of words which would always return 1
  83.   {
  84.     num_words++; // same as above, changed to ++
  85.   }
  86.   return num_words;
  87. }
  88.  
  89. // counting sentences
  90.  
  91. int count_sentences (char* str)
  92. {
  93.   int num_sentences=0;
  94.   for (int i=0; i<strlen(str);i++)
  95.   {
  96.     if (str[i]=='!'||str[i]=='.'||str[i]=='?')
  97.     {
  98.       num_sentences++; // same as above, changed to ++
  99.     }
  100.   }
  101.   return num_sentences;
  102. }
  103.  
  104. //calculating index
  105. int calculate_index (int s, int w, int l)
  106. // I made it s w l to make it way more readable and line length doesn't get too long
  107. {
  108.   int L = l/w*100; // this would produce unnecessary rounding as type is int so it would be rounded down
  109.   int S = s/w*100; // same issue
  110.   int CLindex= 0.0588 * L - 0.296 * S - 15.8;
  111.   return CLindex;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement