loops3 - Computer Programming TA C162 Repetition in...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Computer Programming TA C162 Second Semester 2008-2009 Repetition in Programs Iterative Statements in C for While Do-while
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Computer Programming TA C162 Second Semester 2008-2009 Problem: Searching for Substring User type a line of text (ending with linefeed i.e \n) and print the number of occurrences of string " the ". Reading characters one at a time Use the getchar() function -- returns a single character. Don't need to store input string; look for substring as characters are being typed. Similar to state machine: Based on characters seen, move toward success state or move back to start state. Switch statement is a good match to state machine.
Background image of page 2
3 Computer Programming TA C162 Second Semester 2008-2009 Substring: State machine matched 't' matched 'th' matched 'the' 't' 'h' 'e' 't' 't' 't' no match other other other other increment count
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Computer Programming TA C162 Second Semester 2008-2009 Flow chart read char match = 0 match = 1 match = 2 if 't', match=1 if 'h', match=2 if 't', match=1 else match=0 if 'e', count++ and match = 0 if 't', match=1 else match=0 T T T F F F
Background image of page 4
5 Computer Programming TA C162 Second Semester 2008-2009 Substring: Code (Part 1) #include <stdio.h> main() { char key; /* input character from user */ int match = 0; /* keep track of characters matched */ int count = 0; /* number of substring matches */
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/14/2010 for the course CS SS ZG653 taught by Professor Shanta during the Spring '09 term at Birla Institute of Technology & Science.

Page1 / 16

loops3 - Computer Programming TA C162 Repetition in...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online