LEC20081003 - Introduction to Computer Programming October...

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

View Full Document Right Arrow Icon
Introduction to Computer Programming October 3, 2008 Required readings: chapter 10. CSC180 Fall 2008, University of Toronto
Background image of page 1

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

View Full Document Right Arrow Icon
Search patterns How to show lines that contain a particular string of characters (e.g., ‘‘int’’ )? UNIX command: grep int grep.c , where int is the pattern, and grep.c is the name of a C source file. Pseudocode: while (there is another line) if (the line contains the string) print the line CSC180 Fall 2008, University of Toronto 1
Background image of page 2
C code: /* grep.c */ #include <stdio.h> #include <string.h> /* for strstr() */ #define MAXLINE 1000 /* maximum input line length */ int getline(char line[], int max); /* find all lines that match a pattern */ int main(int argc, char *argv[]) { char line[MAXLINE]; while (getline(line, MAXLINE) > 0) if (strstr(line, argv[1]) != NULL) printf(‘‘%s’’, line); return 0; } CSC180 Fall 2008, University of Toronto 2
Background image of page 3

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

View Full Document Right Arrow Icon
/* get line into s, return length */ int getline(char s[], int lim) { int c, i; i = 0; s[i++] = c; if (c == ’\n’) s[i++] = c; s[i] = ’\0’; return i; } Running example: [[email protected] ~]$ ./a.out int < grep.c int getline(char line[], int max); int main(int argc, char *argv[]) printf(‘‘%s’’, line); CSC180 Fall 2008, University of Toronto 3
Background image of page 4
/* get line into s, return length */ int getline(char s[], int lim) int c, i; Exercise: modify the above code such that the program also prints a line number before each matched line. Try the UNIX command grep -n int grep.c for an output example. Check the description of the library function strstr using this UNIX command: man strstr Exercise: find out what strstr does. CSC180 Fall 2008, University of Toronto 4
Background image of page 5

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

View Full Document Right Arrow Icon
Scope is the region within which a variable can be referenced (i.e., is visible). 1. File scope : from the point of definition or declaration to the end of the file. Examples: external variables and static external variables. 2. Block scope : from the point of definition or declaration to the end of the block. Examples: local variables, function parameters, and static internal variables. CSC180 Fall 2008, University of Toronto
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 27

LEC20081003 - Introduction to Computer Programming October...

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

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