Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Nick Ku PIC10A Intro. to Programming
- ID: 304491540 Spring 2016
- Email: cinestra@ucla.edu HW #3
- Section: 3A
- Honesty Pledge:
- I, Nichokas Ku, pledge that this is my own independent work, which conforms to
- the guidelines of academic honesty as described in the course syllabus.
- List of none bugs: None.
- */
- #include<iostream>
- #include<iomanip>
- #include <string>
- using namespace std;
- bool is_vowel(string letter)
- // Taken from lecture slides
- {
- if (letter.length() > 1)
- return false;
- if (letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u" || letter == "y")
- return true;
- else if (letter == "A" || letter == "E" || letter == "I" || letter == "O" || letter == "U" || letter == "Y")
- return true;
- else
- return false;
- }
- bool is_punctuation(string letter)
- // Exactly the same as is_vowel but with different characters and less cases
- {
- if (letter.length() > 1)
- return false;
- if (letter == "." || letter == "!" || letter == ";" || letter == ":")
- return true;
- else
- return false;
- }
- int number_of_sentences(string phrase)
- // Returns the number of sentences from the whole phrase by counting the number of punctuations
- {
- int count = 0;
- for (int i = 0; i<phrase.length(); i++)
- {
- if (is_punctuation(phrase.substr(i, 1)))
- count++;
- }
- return count;
- }
- int number_of_words(string phrase)
- // Returns the number of words from the whole phrase by counting the number of spaces and adding one
- {
- int spaces = 0;
- for (int i=0; i < phrase.length(); i++)
- {
- if ((phrase.substr(i, 1) == " ") && (phrase.substr(i+1, 1) != " "))
- {
- // Increments the number of spaces whenever a space is found
- // Will not increment if the following space is also blank
- spaces++;
- }
- }
- return spaces+1;
- }
- string next_word(string phrase)
- // Returns the next word in the string phrase by taking the substring from 0 to the next open space or taking the substring from 0 to the end length
- // Runs into a problem when there are two spaces next to each other since it will count one space character by itself as a letter
- // Resolve this problem later by setting the number of syllables for that one space character equal to zero
- {
- string phrase_word;
- int i = 0;
- while (((phrase.substr(i, 1) != " ") || !(is_punctuation(phrase.substr(i,1)))) && (i<phrase.length()))
- {
- i++;
- }
- phrase_word = phrase.substr(0, i);
- // Using i-1 instead of simply i because i locates the space whereas i-1 locates the letter before the space
- return phrase_word;
- }
- int number_of_syllables(string word)
- {
- int n_syllables = 0;
- string prev_char;
- for (int i = 0; i < word.length(); ++i)
- {
- string cur_char = word.substr(i, 1);
- if (i == 0) prev_char = "b";
- else prev_char = word.substr(i - 1, 1);
- if ((cur_char == "e") && (i == word.length()))
- n_syllables = n_syllables + 0;
- // Do nothing
- else if ((is_vowel(cur_char)) && !(is_vowel(prev_char)))
- n_syllables++;
- }
- return n_syllables;
- }
- int readability_index(int nsyllables, int nwords, int nsentences)
- {
- double x = nsyllables;
- double y = nwords;
- double z = nsentences;
- double index = 206.835 - 84.6*(x / y) - 1.015 * (y / z);
- return index;
- }
- int main()
- {
- string phrase;
- string first_word;
- string phrase_copy;
- cout << "Please enter a phrase: " << "\n";
- getline(cin, phrase);
- phrase_copy = phrase;
- long count = 0;
- while (phrase_copy.length() > 0)
- {
- first_word = next_word(phrase_copy);
- long a = number_of_syllables(first_word);
- long b = first_word.length();
- phrase_copy = phrase_copy.substr(b, phrase_copy.length());
- count = count + a;
- }
- int nSyllables = count;
- // Recasts count into an integer
- int nWords = number_of_words(phrase);
- int nSentences = number_of_sentences(phrase);
- int index = readability_index(nSyllables, nWords, nSentences);
- cout << "number of syllables is: " << nSyllables << "\n";
- cout << "number of words is: " << nWords << "\n";
- cout << "number of sentences is: " << nSentences << "\n";
- cout << "readibility index is : " << index << "\n";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement