Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus.

Similar presentations


Presentation on theme: "2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus."— Presentation transcript:

1 2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus Plus Scanning for WordsScanning for Words

2 Recap Finished the movieFinished the movie Recap Character I/ORecap Character I/O The getline() functionThe getline() function Converting stringsConverting strings 2/22/2016 2R. Smith - University of St Thomas - Minnesota

3 2/22/2016 3R. Smith - University of St Thomas - Minnesota Number conversion Variables: total, ones, tensVariables: total, ones, tens Loop while there are digitsLoop while there are digits –Multiply last result by 10 to get ‘tens’ –Subtract ‘0’ from next digit to get the ‘ones’ –Add ‘ones’ and ‘tens’ to get the running total When out of digits, return the totalWhen out of digits, return the total

4 Plus Plus OPTIONAL shortcuts in COPTIONAL shortcuts in C We can add doubled operators to increment or decrement variables:We can add doubled operators to increment or decrement variables:X++X-- All operators work in assignment statementsAll operators work in assignment statements X += 10 // adds 10 to x Y /= 10 // divides Y by 10

5 Assignment 10 Type in lines of text ended by a blank lineType in lines of text ended by a blank line Count the number of words in the linesCount the number of words in the lines At the end, generate a histogram of word lengthsAt the end, generate a histogram of word lengths 2/22/2016 5R. Smith - University of St Thomas - Minnesota

6 2/22/2016 6R. Smith - University of St Thomas - Minnesota Finding and Counting Words Counting CharactersCounting Characters –We just count the number of getchars() we do Counting LinesCounting Lines –We just count the number of ‘\n’ characters Counting WordsCounting Words –Words are separated by ‘white space’ –Can we just count white spaces?

7 2/22/2016 7R. Smith - University of St Thomas - Minnesota Counting Word Separators int sepcount(char in[LINSZ]) { int nw = 0; // number of words int nw = 0; // number of words int i = 0;// loop index int i = 0;// loop index // Loop searches for words while (in[i] != ‘\0’) { while (in[i] != ‘\0’) { if (in[i] == ' ' || in[i] == '\t') { if (in[i] == ' ' || in[i] == '\t') { nw = nw + 1; nw = nw + 1; } i = i + 1; i = i + 1;} // return the result return nw; }

8 2/22/2016 8R. Smith - University of St Thomas - Minnesota Reporting Word Length Sample program:Sample program: We type in a line of textWe type in a line of text For each word, we print out its lengthFor each word, we print out its length Input: a rusty tattered cogInput: a rusty tattered cog Output: 1 - 5 - 8 – 3 -Output: 1 - 5 - 8 – 3 - How do we do this?How do we do this?

9 2/22/2016 9R. Smith - University of St Thomas - Minnesota Identifying Word boundaries For each char: are we inside or outside a word?For each char: are we inside or outside a word? Use ‘states’ and a comb structureUse ‘states’ and a comb structure –One level of ‘if’ checks for white space –One level of ‘if’ checks the ‘state’ Input = White Space Input = other character State = IN Next State = OUT [ended a word] Next State = IN [inside a word] State = OUT Next State = OUT [outside a word] Next State = IN [started a word]

10 2/22/2016 10R. Smith - University of St Thomas - Minnesota Comb structure for word boundaries if (ch == ‘ ‘ || ch == ‘\t’ || ch == ‘\n’) { if (state == OUT) { if (state == OUT) { // handle text outside of a word } else { // found the end of a word state = OUT; } } else { // not white space if (state == OUT) { // found the start of a word state = IN; } else { // handle text inside of a word }}

11 2/22/2016 11R. Smith - University of St Thomas - Minnesota How do we count word length? When we start a word, it’s 1 char longWhen we start a word, it’s 1 char long When we’re inside a word,When we’re inside a word, –we add another char to the length When we end a word,When we end a word, –we don’t add another char –we save the result Input = White Space Input = other character State = IN Next State = OUT Next State = IN State = OUT Next State = OUT Next State = IN

12 2/22/2016 12R. Smith - University of St Thomas - Minnesota How do we count word length? When we start a word, it’s 1 char longWhen we start a word, it’s 1 char long When we’re inside a word,When we’re inside a word, –we add another char to the length When we end a word,When we end a word, –we don’t add another char –we save the result Input = White Space Input = other character State = IN Next State = OUT Print the count Next State = IN Increment count State = OUT Next State = OUT do nothing Next State = IN count = 1

13 2/22/2016 13R. Smith - University of St Thomas - Minnesota Printing Word Lengths – part 1 void wlengths(char in[]) { int i = 0;// loop index int state = OUT;// state variable cc = 0;// character count for a word do { if (isspace(in[i]) { // check for the end of a word if (state == IN) { printf(“ %d – “, cc); state = OUT; }

14 2/22/2016 14R. Smith - University of St Thomas - Minnesota Printing Word Lengths – part 2 } else { // not white space // check for the start of a word // check for the start of a word if (state == OUT) { if (state == OUT) { cc = 1; state = IN; // handle text inside of a word // handle text inside of a word } else { } else { cc = cc + 1; }} i = i + 1; } while (in[i] != ‘\0’); printf(“ %d \n“, cc); }

15 2/22/2016 15R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus."

Similar presentations


Ads by Google