CS计算机代考程序代写 compiler ECE209_strings.pptx

ECE209_strings.pptx

ECE 209 Fall 2012 Strings

9/24/2012′ 1′

String’

• Sequence’of’characters'(stored’in’an’array)’
• Terminated’by’null’character'(‘\0′)’

Working’with’strings’

• AllocaFng’and’iniFalizing’
• PiIalls:’array’vs.’pointer’

• Standard’Library’funcFons’
• strlen(),’strcpy(),’…’

•  I/O’
Read:
Chapter 11, C Primer Plus

ECE 209 Fall 2012 Strings

9/24/2012′ 2′

There is no “string” type in C.

A string is stored as an array of characters.
The null character (ASCII 0) terminates the
string.

char str[5]; ‘a’ ‘b’ ‘c’ ‘\0’ “abc”

‘a’ ‘\0’ “a”

‘a’ ‘b’ ‘c’ ‘d’ ‘\0’ “abcd”

‘\0′ “”

str[0] str[4]

The array is
the container.

The string is
the content.

ECE 209 Fall 2012 Strings

9/24/2012′ 3’

There are no string operators in C.

Integer operators (+, -, &, …) and assignment
operator work on elements (characters) of
the string.

str[0] = ‘a’;
str[1] = ‘b’;
str[2] = ‘c’;
str[3] = 0;

str = “abc”;

Likewise, there are no array operators.
Can only perform operations on the
elements of an array.

ECE 209 Fall 2012 Strings

9/24/2012′ 4′

String arrays vs. string literals.

String literals (e.g., “abc”) are stored somewhere
in memory by the compiler, but programmer
doesn’t know where. They are constant, and
cannot be changed.

char *promptStr;

promptStr = “Enter a number: “;

promptStr[0] = ‘e’; This will likely cause a

segmentation fault.

Allocated somewhere in memory,
but probably protected.

ECE 209 Fall 2012 Strings

9/24/2012′ 5′

String initialization

char str[5] = “abc”; ‘a’ ‘b’ ‘c’ ‘\0’ ‘\0’

str[0] str[4]

Can use a string literal to initialize the contents
of a char array.

char str[5] = {‘a’, ‘b’, ‘c’, ‘\0’};

means the same as…

NOTE: String literal includes the terminating null character.

‘\0′ and 0 are the same value.

ECE 209 Fall 2012 Strings

9/24/2012′ 6’

String initialization

char str[] = “abc”; ‘a’ ‘b’ ‘c’ ‘\0’

Just like other arrays, can leave off array size
if initializer provided. Space allocated will be enough
to hold initial data.

char str[] = {‘a’, ‘b’, ‘c’, ‘\0′};

means the same as…

ECE 209 Fall 2012 Strings

9/24/2012′ 7′

Array vs. Pointer

Make sure you understand the difference
between these two:

char str[5] = “abc”;

char *str = “abc”;

How much space is allocated?
Where are the characters stored?
What can you do with the pointer str?

ECE 209 Fall 2012 Strings

9/24/2012′ 8′

const Type Qualifier

To emphasize the difference, and to let the
compiler help us avoid mistakes, we declare a
“pointer to a constant character”:

const char *str = “abc”;

The pointer (str) is not constant. It can be changed.
But the character it points to cannot be changed.

If we declare it this way, the compiler will complain
if we try to assign to a character via this pointer.
(Instead of waiting for something bad to happen at runtime.)

ECE 209 Fall 2012 Strings

9/24/2012′ 9′

Using Pointer Notation for Strings

char str[5] = “abc”;
char *p = str; /* point to first char*/

int count = 0; /* count non-null chars */

/* three different versions of loop */
while (*p != 0) { count++; p++; }

while (*p++ != 0) count++;

while (*p++) count++;

This one’s a little
different. Where will
p point after the loop?

ECE 209 Fall 2012 Strings

9/24/2012′ 10’

Another Example:
Copy from string t to string s
char s[20];
char t[20];
char *sp = s, *tp = t;
/* assume that t has data */
while (*tp != 0) {
*sp = *tp;
sp++; tp++;
}
*sp = ‘\0’; /* shorter version */

while (*tp) *sp++ = *tp++;
*sp = ‘\0′;

ECE 209 Fall 2012 Strings

9/24/2012′ 11’

Standard Library:

size_t strlen(const char *s);
return number of chars in string s (not counting null at the end)

char *strcpy(char *s, const char* t);
copy characters from t to s, including ‘\0’; return s

char *strncpy(char *s, const char *t, size_t n);
copy at most n characters from t to s; return s
pad with ‘\0’ if t has fewer than n characters

int strcmp(const char *s, const char *t);
compare s to t; return <0 if s < t, 0 if s == t, >0 if s > t

ECE 209 Fall 2012 Strings

9/24/2012′ 12′

String comparison?
Characters are compared to one another
(as unsigned char) until a difference is found,
or until the end of the strings.

Examples:

“A” less than “a” 0x41 ‘A’ < 0x61 'a' "abc" less than "abcd" 0x0 '\0' < 0x64 'd' "aBc" less than "abc" 0x42 'B' < 0x62 'b' "abc" less than "cat" 0x61 'a' < 0x63 'c' "Cat" less than "abc" 0x43 'C' < 0x61 'a' "abc" equal to "abc" All match. ECE 209 Fall 2012 Strings 9/24/2012' 13' More Standard Library:

char *strchr(const char *s, char c);
return pointer to first occurrence of c in s, NULL if not present

char *strrchr(const char *s, char c);
return pointer to last occurrence of c in s, NULL if not present

char *strcat(char *s, const char *t);
concatenate t to s — i.e., copy characters from t to the end of s;
return s

ECE 209 Fall 2012 Strings

9/24/2012′ 14′

Dangers with functions
When copying from one string to another, must make
sure that destination string has enough space for
characters, including ‘\0’.

char s[5];
char t[20];
strcpy(s,t); /* BAD IDEA!!! */

Undefined behavior when strings overlap.

char str[] = “abcdefghijk”;
char *s = &str[3];
char *t = &str[0];
strcpy(s,t); /* What happens? */

ECE 209 Fall 2012 Strings

9/24/2012′ 15′

printf and scanf
The %s format code is used for strings.
In both cases, corresponding argument is char*.

printf — Prints characters until ‘\0’.
scanf — Reads non-whitespace characters, stores into
string array; ‘\0′ will be added at the end.

char name[] = “Spartacus”;

printf(“My name is %s!\n”, name);
scanf(“%s”, name);

Look — no ampersand!!!
The variable name is already a pointer.

ECE 209 Fall 2012 Strings

9/24/2012′ 16’

Danger with string input
What happens if array is not big enough to
hold the characters typed by user?

char name[5];
scanf(“%s”, name);

What if user types “Spartacus”?

Good idea to LIMIT the characters read by scanf,
using the optional field width format:

scanf(“%4s”, name);

Will read at most 4 characters, and ‘\0′ added.
In case above, name will get “Spar”.

ECE 209 Fall 2012 Strings

9/24/2012′ 17’

Other String Input Function

char *gets(char *s);
Reads from stdin.
Up to and including linefeed. (Linefeed not stored in string.)
String is terminated with ‘\0’.
No way to limit size.
Return pointer to string (same as passed in), or NULL if error.

This is a more dangerous function.
Recommend using scanf instead, to limit
problems with overrunning the string array.

But it can be useful when reading a “line” of
text that includes spaces/tabs. Just be sure
you know the max number of characters that
may be read.