I hope you: - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

I hope you:

Description:

1. I hope you: Have turned in Proj-2 due now. Are preparing for ... scanf() uses NO ampersand (&) for string variables (array name), unlike ordinary variables ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 29
Provided by: vana9
Category:
Tags: ampersand | hope

less

Transcript and Presenter's Notes

Title: I hope you:


1
CS110 Lecture 14Arrays For Strings
Jack Tumblinjet_at_cs.northwestern.edu
  • I hope you
  • Have turned in Proj-2due now
  • Are preparing for Midterm Exam Wed Oct 29

2
(Recall) Arrays and Functions
  • void cleanArray2(int siz, int a) /fcn
    prototype/...
  • int main(void)
  • int count5 5,4,3,2,1
  • cleanArray(5, count) / use array name /
  • printArray(5, count)
  • return 0
  • void cleanArray2int siz, int a) / fcn body
    /
  • / clear 1st siz elements /
  • int i
  • for (i0 iltsiz i) ai0

3
(Recall) Arrays and Functions
  • void cleanArray(int siz, int a)
    /prototype/...
  • int main(void)
  • int count5 5,4,3,2,1
  • cleanArray(5, count)
  • printArray(5, count)
  • return 0
  • Array name count gives arrays 0th byte location,
  • function cleanArray() copies that address to
    define its own local array named a , so that
  • a array starts at same memory location as count!

4
(Recall) Arrays and Memory
a
...4269427042714272427342744275427642774
2784279428042814282...
count
count0
  • While cleanArray() runs, its a array uses the
    same memory locations use by the count array.
  • Any changes to a arrayare changes to count.

count1
Address ? ? ?
count2
count3
5
Strings The 2nd Big Idea
  • Text Computing gt Math Computing (for most
    of us)
  • NOW were ready for it!
  • we have interfaces/libraries,
  • arrays / indexing / memory addresses
  • It soon leads to a 3rd Big Idea pointers
  • Chap.9 does NOT give you the whole story

6
Strings The 2nd Big Idea
  • CS Jargon a string
  • Is a single piece of text made of char values,
  • Any desired length,
  • Any desired characters (except one),
  • Any desired order.
  • String Examples
  • asldk2 2_at_ 2 _VA12 78
  • The night has d eyes\n
  • ?THIS is not my beautiful life! gtTalkingHeads

7
We have already have
single quotes
  • Character constants
  • 'a' 'R' '9' '\n' '\t'
  • Character variables
  • char ans 'y'
  • String constants
  • "average value is f\n" "d of your base
    are belong to us"
  • "hit any key to continue"
  • ?String Variables? How?

double quotes
8
Strings in Memory
...4269427042714272427342744275427642774
2784279428042814282...
h e l l o w o r l d ! NU
LL
  • in the C language,
  • A string is a list of char values,
  • stored sequentially in memory,
  • always ended by a NULL
    char value.

Address ? ? ?
9
Strings in Memory
  • ?!?! NULL ?!?!
  • an unprintable special character (see Appendix)
  • Why? NULL is a sentinel it means end of the
    string!
  • How? in C, NULL character constant is also \0
  • What? the stored numerical value for NULL is
    zero
  • char ch2
  • ch0 \0 /set both to NULL /
  • ch1 NULL / (same as \0 character /
  • printf(NULL is d,d,(int)ch0,(int)ch1)
  • produces
  • gt NULL is 0,0

(cast it from char to integer)
10
String Variables
  • (Recall) A variable is
  • a place-holder, a named chunk of memory to hold
    just one value that can change
  • A string is a sequential list of char values,
    that ends at NULL (\0).
  • Thus an array of type char is one
    way to make a string variable
  • (but array size is fixed the string must fit
    inside)
  • Lets try it

11
Put String in a char Array I
msg
...4269427042714272427342744275427642774
2784279428042814282...
h e l l o w o r l d ! \
0
  • int main()
  • char msg20
  • msg0 h
  • msg1 e
  • msg2 l
  • msg3 l
  • msg4 o
  • msg5 \0
  • ... / WHY is this so tedious?!? /
  • printf(s\n,msg)

uses char literals fill the char array with char
constants
Address ? ? ?
result gt hello world!
12
Put String in a char Array II
msg
...4269427042714272427342744275427642774
2784279428042814282...
h e l l o w o r l d ! \
0
  • int main()
  • char msg20h,e,l,l,o,
    , w,o,r,l,d,\0
  • printf(s,msg)
  • / not much better... /
  • result
  • gt hello world!

Address ? ? ?
uses char literals fill the char array with char
constants
13
Put String in a char Array III
msg
...4269427042714272427342744275427642774
2784279428042814282...
h e l l o w o r l d ! \
0
  • int main()
  • char msg20hello world!
  • printf(s,msg)
  • / AH! thats the way!... /
  • result
  • gt hello world!

Address ? ? ?
uses a string literal double-quotes make a
string constant
14
Strings in Arrays
  • A char array is a fairly good string variable,
    and you can initialize it with a string
    constant
  • char msg80 one line of text.

15
Strings in Arrays
  • A char array is a fairly good string variable,
    and you can initialize it with a string
    constant
  • char msg80 one line of text.
  • Print a string variable using s and printf()
    printf(You typed s, msg)

16
Strings in Arrays
  • A char array is a fairly good string variable,
    and you can initialize it with a string
    constant
  • char msg80 one line of text.
  • Print a string variable using s and printf()
    printf(You typed s, msg)
  • Read a string variable using s and scanf()
    printf(Type answer, press return) scanf(
    s,msg)

17
Strings in Arrays
SURPRISE! scanf() uses NO ampersand () for
string variables (array name), unlike ordinary
variables WHY? --array name is an addressof
the 0th array element --for ordinary variable
var, writing var means give me the
address of var
  • A char array is a fairly good string variable,
    and you can initialize it with a string
    constant
  • char msg80 one line of text.
  • Print a string variable using s and printf()
    printf(You typed s, msg)
  • Read a string variable using s and scanf()
    printf(Type answer, press return) scanf(
    s,msg)

18
Basic String Variable Actions
  • How can I . . .
  • Assign copy a string from one array to another?
  • Append copy one string onto the end of another?
  • Find find a given character in a string? (find
    all e)
  • Parse find the 1st,2nd,3rd... Nth word in a
    string?
  • Match find a given key word in a string?
  • Delete remove one character from a string?
  • Erase remove one word from a string?
  • You know how!---with loops, if/else, , etc.

19
Strings in Arrays
  • Problem Many common tasks are tedious
  • copy a string constant to a string variable
  • append one string to the end of another
  • remove the first part or last part of a string .
    . .
  • Solution function library for tedious tasks
    includeltstring.hgt / see pg 666 in book /
  • Side note cslibvc.lib replaces string.h (a
    big part of C) with his strlib.h (NOT a part
    of C) BAD IDEA!

20
Solution include ltstring.hgt
  • Use the string.h library to do the tedium a
    vital part of C (see book, pg. 666 for summary)
  • include ltstdio.hgt / for printf() /include
    ltstring.hgt / for strcpy() /int main()char
    msg20
  • .
  • strcpy(msg,"Hello world!")
  • printf("my strings\n",msg)
  • Result gt my stringHello world!
  • gt

(Recall)String literal text in quotes
s means a string OK for scanf() too.
21
Solution include ltstring.hgt
  • Book neglects it, but string.h has everything you
    need
  • Find string length
  • Copy a string
  • Join 2 strings
  • Compare 2 strings
  • Find a char in a string
  • Find a string in a string

(later)
22
String Length int strlen(src)
  • include ltstdio.hgt / for printf() /
  • include ltstring.hgt / for strlen() /int
    main()char msg20 "Hello world!"
  • cnt strlen(msg)
  • printf("length of \"s\" is
    d\n",msg,cnt)
  • Result
  • gt length of "Hello world!" is 12
  • gt

23
String Copy strcpy(dest, src)
  • include ltstdio.hgt / for printf() /include
    ltstring.hgt / for strcpy() /int
    main()char msg20
  • strcpy(msg,"Hello world!")
  • printf("my strings\n",msg)
  • Result
  • gt my string Hello world!
  • gt

strcpy() is the string assignment operator
24
String Copy strncpy(dest,src,cnt)
  • include ltstdio.hgt / for printf() /include
    ltstring.hgt / for strncpy()/int
    main()char msg20
  • strcpy(msg,"Hello world!", 3)
  • printf("my strings\n",msg)
  • Result
  • gt my string Hel
  • gt

Copy only 1st 3 chars
25
String Merge strcat(str1,str2)
  • include ltstdio.hgt / for printf() /
  • include ltstring.hgt / for strcat() /int
    main()char msg181,msg281
  • int diff
  • strcpy(msg1,"Hello you!")
    strcpy(msg2,"Hello me!")
  • diff strcat(msg1,msg2)
  • printf("s\n", msg1)
  • Result
  • gt Hello you!Hello me!
  • gt

msg1 is BOTH input and output to strcat() fcn!
26
String Merge strncat(str1,str2,cnt)
  • include ltstdio.hgt / for printf() /
  • include ltstring.hgt / for strncat()/int
    main()char msg181,msg281
  • strcpy(msg1,"Hello you!")
    strcpy(msg2,"Hello me!")
  • strncat(msg1,msg2,3)
  • printf("s\n", msg1)
  • Result
  • gt Hello you!Hel
  • gt

msg1 is BOTH input and output to strcat() fcn!
27
String Compare int strcmp(a,b)
  • include ltstdio.hgt / for printf() /
  • include ltstring.hgt / for strcmp() /int
    main()char msg181 "Hello to you!"char
    msg281 "Hello to me!"
  • int diff diff
    strcmp(msg1,msg2)
  • if(0diff) printf("same!\n")
  • else printf("different!\n")
  • Result
  • gt different!
  • gt

28
String Compare int strncmp(a,b,cnt)
  • include ltstdio.hgt / for printf() /
  • include ltstring.hgt / for strncmp()/int
    main()char msg181 "Hello to you!"char
    msg281 "Hello to me!"
  • int diff
  • diff strncmp(msg1,msg2,6)
  • if(0diff) printf("same!\n")
  • else printf("different!\n")
  • Result
  • gt same!
  • gt

compare only the 1st 6 chars
Write a Comment
User Comments (0)
About PowerShow.com