Skip to content

Instantly share code, notes, and snippets.

@wx5162839
Created April 25, 2020 03:06
Show Gist options
  • Select an option

  • Save wx5162839/60d20e8c5b01a0161e2f1fe253fc0ca9 to your computer and use it in GitHub Desktop.

Select an option

Save wx5162839/60d20e8c5b01a0161e2f1fe253fc0ca9 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int count_letters(string s);
int count_words(string s);
int count_sentences(string s);
int main(void)
{
string text=get_string("Text: ");
int letters = count_letters(text);
printf("%i letter(s)\n", letters);
int words = count_words(text);
printf("%i word(s)\n", words);
int sentences = count_sentences(text);
printf("%i sentence(s)\n", sentences);
//make sure the same type otherwise it will be not precise; float make sure everywhere is float.
float L = (float)letters*100/(float)words;
float S = (float)sentences*100/(float)words;
float index = 0.0588*L-0.296*S-15.8;
int index1 = (int)(index+0.5);
if(index>=16)
{
printf("Grade 16+");
}
if(index<1)
{
printf("Before Grade 1");
}
if(index>=1&&index<16)
{
printf("Grade %i", index1);
}
printf("\n");
}
int count_letters(string s)
{
int sum=0;
for(int i=0,n=strlen(s);i<n;i++)
{
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))
{
sum++;
}
}
return sum;
}
int count_words(string s)
{
int sum=0;
for(int i=0,n=strlen(s);i<n;i++)
{
if(s[i]==' ')
{
sum++;
}
}
return sum+1;
}
int count_sentences(string s)
{
int sum=0;
for(int i=0,n=strlen(s);i<n;i++)
{
if(s[i]=='.'||s[i]=='!'||s[i]=='?')
{
sum++;
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment