CS 151 - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

CS 151

Description:

Programming assignment #6 is due before ... Be sure to check your score posting (assignment 5 should show up ... used for sorting words alphabetically. ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 32
Provided by: paul313
Category:

less

Transcript and Presenter's Notes

Title: CS 151


1
CS 151
  • Quiz 5 is available through midnight tonight.
  • Programming assignment 6 is due before midnight
    tonight.
  • Programming assignment 7 is described now. Due
    monday after thanksgiving break.
  • Be sure to check your score posting (assignment 5
    should show up sometime this afternoon)

2
Strings
  • A string is an array of characters
  • char myString30 / memory allocated, no chars
    /
  • char yourString / no memory allocated /
  • char herString / no memory allocated /
  • char hisString "hee hee hee"
  • / memory allocated, chars assigned, '\0'
    appended
  • char theirString "no no no"
  • / memory allocated, chars assigned, '\0'
    appended

3
int scanf(const char , ...)
  • char a80
  • scanf("s", a) / could use scanf("s", a0)
    /
  • Reads from KB until white space encountered.
    Copies into a.
  • If you type "Go Beavers!!" as input, a
    will have only "Go".
  • scanf can be used only to input single words.

4
char gets(char )
  • char a80
  • gets(a)
  • Copies characters from keyboard into a up to
    but not including the newline ('\n').
  • Copies everything including spaces.
  • Puts '\0' at end.
  • Must be enough room in a for the input!
  • C does not check for going past the end of the
    array

5
int strlen(const char s)
  • char s "This string is 28 chars long"
  • printf("d", strlen(s))
  • Prints the value 28
  • Does not count the '\0' at the end.

6
char strcpy(char s, const char t)
  • char s10, t "12345"
  • strcpy(s, t) / copies into s from t /
  • printf("s", s)
  • /or printf("s", strcpy(s,t)) /
  • prints the string "12345"
  • You must make sure s is big enough to hold all
    of the characters of t!
  • e.g., if declared s3, then BIG TROUBLE.

7
int strcmp(const char s, const char t)
  • char s"smith", t"smyth"
  • n strcmp(s, t) / n is negative /
  • n strcmp(t, s) / n is positive /
  • n strcmp(s, s) / n is zero, /
  • This function is used for sorting words
    alphabetically.
  • Note that strcmp ("Smith", "smith") is negative,
    i.e. "Smith" is less than "smith".

8
char strcat(char s, const char t)
  • char s20, t "bea", u "vers"
  • strcpy(s, t) / copies t to s /
  • strcat(s, u) / s now contains "beavers" /
  • Appends (concatenates) second string onto end of
    first (starting at '\0' position).
  • You must have room in s or again there will be
    TROUBLE.
  • If s3 declared, it wont work (sometimes).

9
char strchr(const char s, int ch) char
strrchr(const char s, int ch)
  • Searches s for a character and returns pointer to
    the character -- strchr() from the start,
    strrchr() from the end.
  • char s "The quick sly beaver jumped over the
    lazy white duck"
  • strchr(s, 'y') points to here.
  • strrchr(s, 'y') points to here.

10
char strstr(const char s, const char t)
  • Searches s for a substring, t and returns pointer
    to the beginning of the substring
  • char s "The quick sly beaver jumped over the
    lazy white duck"
  • strstr(s, "jumped") points to here.

11
  • int main (void)
  • char a80 int count 0
  • while(gets(a)) count word_count(a) / get
    lines from KB and count /
  • printf("d\n", count) printf("d\n", count)
  • return 0
  • int word_count(const char s)
  • int c 0 / initialize word count /
  • while (s ! '\0') / loop until terminal null
    character found /
  • while (isspace(s)) s / skip white
    space /
  • if(s ! '\0') / make sure not at end
    /
  • c / found non-space so start of word
    /
  • while (!isspace(s) s ! '\0') s
    / increment thru word /
  • return c

Counting Words
big enough?
needed because of bug in CodeWarrior.
returns NULL pointer on error or EOF
12
Starting (c 0)
cs151 is a really tough course!
s
  • int word_count(cons char s)
  • int c 0 / initialize word count /
  • while (s ! '\0') / loop until terminal null
    character found /
  • while (isspace(s)) s / skip white
    space /
  • if(s ! '\0') / make sure not at end
    /
  • c / found non-space so start of word
    /
  • while (!isspace(s) s ! '\0') s
    / increment thru word /
  • return c

13
Start of first word (c 0)
cs151 is a really tough course!
s
  • int word_count(cons char s)
  • int c 0 / initialize word count /
  • while (s ! '\0') / loop until terminal null
    character found /
  • while (isspace(s)) s / skip white
    space /
  • if(s ! '\0') / make sure not at end
    /
  • c / found non-space so start of word
    /
  • while (!isspace(s) s ! '\0') s
    / increment thru word /
  • return c

14
Finished first word (c 1)
cs151 is a really tough course!
s
s
  • int word_count(cons char s)
  • int c 0 / initialize word count /
  • while (s ! '\0') / loop until terminal null
    character found /
  • while (isspace(s)) s / skip white
    space /
  • if(s ! '\0') / make sure not at end
    /
  • c / found non-space so start of word
    /
  • while (!isspace(s) s ! '\0') s
    / increment thru word /
  • return c

15
processing spaces (c 1)
cs151 is a really tough course!
s
s
int word_count(cons char s) int c 0 /
initialize word count / while (s ! '\0') /
loop until terminal null character found /
while (isspace(s)) s / skip white space /
if(s ! '\0') / make sure not at end
/ c / found non-space so start of
word / while (!isspace(s) s ! '\0')
s / increment thru word / return
c
16
2nd word (c 2)
cs151 is a really tough course!
s
int word_count(cons char s) int c 0 /
initialize word count / while (s ! '\0') /
loop until terminal null character found /
while (isspace(s)) s / skip white space /
if(s ! '\0') / make sure not at end
/ c / found non-space so start of
word / while (!isspace(s) s ! '\0')
s / increment thru word / return
c
17
2nd word end (c 2)
cs151 is a really tough course!
s
s
int word_count(cons char s) int c 0 /
initialize word count / while (s ! '\0') /
loop until terminal null character found /
while (isspace(s)) s / skip white space /
if(s ! '\0') / make sure not at end
/ c / found non-space so start of
word / while (!isspace(s) s ! '\0')
s / increment thru word / return
c
18
lots of space (c 2)
cs151 is a really tough course!
s
s
int word_count(cons char s) int c 0 /
initialize word count / while (s ! '\0') /
loop until terminal null character found /
while (isspace(s)) s / skip white space /
if(s ! '\0') / make sure not at end
/ c / found non-space so start of
word / while (!isspace(s) s ! '\0')
s / increment thru word / return
c
19
last word (c 6)
cs151 is a really tough course!
s
s
int word_count(cons char s) int c 0 /
initialize word count / while (s ! '\0') /
loop until terminal null character found /
while (isspace(s)) s / skip white space /
if(s ! '\0') / make sure not at end
/ c / found non-space so start of
word / while (!isspace(s) s ! '\0')
s / increment thru word / return
c
20
finished (c 6)
cs151 is a really tough course!
s
int word_cnt(char s) int cnt 0 /
initialize word count / while (s ! '\0') /
loop until terminal null character found /
while (isspace(s)) s / skip white space /
if(s ! '\0') / make sure not at end
/ cnt / found non-space so start of
word / while (!isspace(s) s ! '\0')
s / increment thru word / return
cnt
21
string declaration and memory
char a3
char p
char p "abc"
char a "abc"
p
p
a
a b c \0
??? ??? ???
a
???
a b c \0
22
constant and variable strings
  • char p, a3, b "abc"
  • p "xyz" / OK. Assigns pointer to p /
  • a "xyz" / NO, a is a constant /
  • a "xyz" / NO, syntax error /
  • p b / OK. b (pointer) assigned to p /
  • a p / NO, involves copying all elements,
  • use strcpy()/
  • a b / NO, same problem as above /

23
An array of strings
  • char s "one", "two", "three"
  • This is an array of pointers that point to arrays
    of characters, thus it is a 2-d array.
  • s0 ?
  • s11 ?
  • s0 ?
  • s ?

24
In memory we have
s0 s1 s2
o n e \0
char s "one", "two", "three"
t w o \0
t h r e e \0
25
Dos/Unix command arguments
  • On command line systems (DOS/Unix) the arguments
    are passed as an array of pointers to characters
  • int main(int argc, char argv)
  • argc - count of number of arguments
  • argv - each argument as a string

26
Array of strings vs 2-d array of char
  • char s "ab", "cd"
  • not the same as
  • char t3 'a','b','\0', 'c','d','\0'
  • Former is an array of pointers pointing to
    strings.
  • Latter is linear sequence of characters.

27
The memory picture
a b \0 c d \0
s0 s1
t0 t1
a b \0 c d \0
char s "ab", "cd" / s0,s1 are
variables/
char t3 'a','b','\0', 'c','d','\0' /
t0, t1 are constants /
28
Printing Words Backwards
  • Use an array of strings (2-D array of char).
  • char s530 / 5 words up to 30 char each /
  • int i
  • Get words from keyboard input
  • for(i 0 i lt 5 i)
  • scanf("s", si)
  • Print them backwards.
  • for(i 4 i gt 0 --i)
  • printf("\ns", si) / run the program /

29
Sorting Words by swapping pointers
  • char s530, p5
  • int i
  • for(i0 ilt5 i)
  • gets(si) / get the words from KB /
  • pi si / initialize pointer array /
  • bubble(p, 5) / sort words using pointers /
  • for(i0 i lt 5 i)
  • printf("s ", pi) / print the words /

30
The new bubble()
  • include ltstring.hgt
  • void bubble(char a, int n)
  • int i, j
  • for(i 0 i lt n-1 i)
  • for(j n-1 j gt i --j)
  • if( strcmp(aj-1,aj) gt 0)
  • swap(aj-1, aj)
  • return

31
The new swap()
note that p is a pointer to (a pointer to char),
i.e. p points to an address which is itself a
pointer to a character.
  • void swap(char p, char q)
  • char tmp
  • tmp q / swap the pointers /
  • q p
  • p tmp
  • return
Write a Comment
User Comments (0)
About PowerShow.com