Data Structures Part 2 (strings)

advertisement
clark
cs1713
syllabus
lecture notes
programming assignments
Strings
Strings are stored as arrays that are null byte terminated.
homework
set up
char szMyName[15] = "Larry Clark";
L
a
r
r
y
C
l
a
r
k
\0
?
We can have arrays of strings (which are actually two-dimensional arrays).
char dayWeekM[7][10] = { "Sunday", "Monday", "Tuesday"
, "Wednesday", "Thursday", "Friday", "Saturday" };
S
M
T
W
T
F
S
u
o
u
e
h
r
a
n
n
e
d
u
i
t
d
d
s
n
r
d
u
a
a
d
e
s
a
r
y
y
a
s
d
y
d
\0
\0
y
d
a
\0
a
?
?
\0
a
y
?
y
?
?
?
y
\0
?
\0
?
?
?
\0
?
?
?
// Using a typedef for the day of the week */
typedef char DayOfWeek[10];
DayOfWeek dayWeekM[7] = { "Sunday", "Monday", "Tuesday"
, "Wednesday", "Thursday", "Friday", "Saturday" };
?
?
string assignments
Strings are assigned values using
strcpy(szTarget, szSource)
assigns szSource to the target
strncpy(sbTarget, szSource, iLength)
assigns iLength characters from szSource to sbTarget. If the
actual length of szSource is > iLength, the result in sbTarget
won't be null terminated. If a null byte is encountered before
iLength, no other characters are copied from szSource.
memcpy(sbTarget, sbSource, iLength)
assigns exactly iLength characters from sbSource to sbTarget.
If the values can overlap, use memmove which works properly
if they overlap.
memset(sbTarget, cChar, iLength)
sets each character in sbTarget for iLength bytes to cChar.
char szWinnerFullName[15] = "Anita Break";
char szContestantFullName[15] = "Bob Wire";
A
n
i
t
a
B
r
e
a
k
\0
?
?
?
strcpy(szWinnerFullName, szContestantFullName);
Value of szWinnerFullName is
B
o
b
W
i
r
e
\0
a
k
\0
?
?
?
char szFeed[] = "dog food";
char szWaste[] = "cat litter";
memcpy(szFeed, szWaste, 3);
Value of szFeed is "cat food";
char szMyString[80] = "shift me two characters";
char *psbFrom;
char *psbTo;
psbFrom = &szMyString[0];
psbTo = &szMyString[2];
memcpy(psbTo, psbFrom, 10);
The result is potentially affected by an overlap.
Resulting value might be
"shshshshshsh characters" or some other strange
combination instead of
"shshift me t characters"
Using memmove(psbTo, psbFrom, 10) would guarantee that
result, but it may be slower.
memset(szMyString, '\0', sizeof(szMyString));
assigned 80 null bytes to szMyString
very important string functions
int strlen(szValue)
returns the length of the null terminated szValue.
int strcmp(szStr1, szStr2)
compares szStr1 with szStr2. If the values are the same up to
the null terminator in each, 0 is returned. If szStr1 < szStr2, a
negative value is returned. If szStr1 > szStr2, a positive value is
returned.
char szName[20] = "Hammond Ecks";
strlen(szName) would return 12; whereas, sizeof(szName)
returns 20.
char szValue1[] = "applet";
char szValue2[] = "appleseed";
strcmp(szValue1, szValue2) returns a positive number
strncmp(szValue1, szValue2, 5) returns 0
int strncmp(szStr1, szStr2, iLength)
compares szStr1 with szStr2 for a maxium of iLength
characters. If it encounters a null character before reaching
iLength characters, the comparsion stops. If the values are the
same, 0 is returned. If szStr1 < szStr2, a negative value is
returned. If szStr1 > szStr2, a positive value is returned.
strcat(szStr1, szStr2)
concatenates szStr1 and szStr2. It does this by appending
szStr2 onto the end of szStr1. Since the result is placed in the
memory of szStr1, make certain it has enough space.
char szFullName[30] = "Anita ";
strcat(szFullName, "Goodgrade");
causes szFullName to contain "Anita Goodgrade".
int strspn(szStr, szChars)
spans szStr looking for the first character which is not in
szChars. It returns the number of characters in szStr that
match any characters in szChars up to that point. If all the
characters are in szChars, it effectively will return the string
length.
int iValidLength;
char szInputSSN[] = "432-11-E233";
iValidLength = strspn(szInputSSN, "0123456789-");
would return 7 which is the number of valid characters before
the "E".
int strcspn(szStr, szChars)
spans szStr looking for the first character which is in szChars.
It returns the number of characters in szStr (up to that point)
that do not match any characters in szChars. If it doesn't find
a character that is in szChars, it effectively will return the
string length.
char *strstr(szStr, szMatch)
finds the first occurrence in szStr of the entire value of
szMatch and returns a pointer to that address. If the value of
szMatch isn't found, NULL is returned.
int iDelim;
char szInputpath[] = "www.cs.utsa.edu/~clark";
iDelim = strcspn(szInputpath, "./~");
would return 3 which is the number of characters before the
".".
What would happen if we execute strcspn(szInputpath, '.') ?
char *pszFound;
char szNursery[] = "hickery dickery dock";
pszFound = strstr(szNursery, "ick");
Warning
char *strtok(szSource, szDelimiters) is a powerful, but sometimes dangerous
function since it uses static memory for storing its current position. Do not
use strtok().
int isalpha(char cValue)
retuns 0 if the character is not an alpha character (a-zA-Z);
otherwise, non-zero (true) is returned.
int isdigit(char cValue)
retuns 0 if the character is not an numeric character (0-9);
otherwise, non-zero (true) is returned.
int isprint(char cValue)
retuns 0 if the character is not a printable character;
otherwise, non-zero (true) is returned.
Warning: some implementations will fail when passed certain nonprintable characters. Later in the course, we will show how to do this
with a macro.
Exercise: Show code for the function countCh(char szString[], char ch)
which counts the number of occurrences of ch in szString and returns
that value functionally.
Exercise: show code for the function countAny(char szString[], char
szMatchChars[]) which returns a count of the characters in szString
that match any of the characters in szMatchChars.
Example:
countAny("Mississippi", "ip") returns 6.
That returns the address of szNursery[1]
Multiple independent uses of strtok can affect the results of each
other.
int countCh(char szString[], char ch)
{
int i;
int iCount = 0;
for (i = 0; i < strlen(szString); i++)
{
if (szString[i] == ch)
iCount++;
}
return iCount;
}
// Use countCh on each of the characters in szMatchChars
int countAny(char szString[], char szMatchChars[])
{
int i;
int iCount = 0;
// iterate over each character in szMatchChars
for (i = 0; i < strlen(szMatchChars); i++)
{
iCount = iCount + countCh(szString, szMatchChars[i]);
}
return iCount;
}
Download