Given a sequence of characters c0 c1 c2 representing Englis
Given a sequence of characters c_0, c_1 c_2, ...., representing English text, use pseudocode to describe an algorithm that counts the number of words in the first sentence. Words are separated by one or more spaces. You may assume that the sentence ends with a period right after the last word. Example: Count: 5 words. Express the running time of your algorithm as a function of n (the number of characters in the first sentence) and then use asymptotic notation to simplify it.
Solution
Pseudo Code
1) Put a counter=1, that will count the number of occurrence of space
2) while(character!=\'.\') // while character is not equal to full stop
{
if(character[i]==\' \' && character[i+1] !=\' \')
counter = counter+1;
}
3) Print the value of counter
Running time of an algorithm depends is worst case(n^2), n for the while loop and n for the inner loop
T(n) = n^2 = O(n^2)
