Data Structures Part 2 Strings
StringsStrings are stored as arrays that are zero byte terminated.
We can have arrays of strings (which are actually two-dimensional arrays). / char szMyName[15] ="Larry Clark";
L / a / r / r / y / C / l / a / r / k / \0 / ? / ? / ?
char dayWeekM[7][10] = { "Sunday", "Monday", "Tuesday"
, "Wednesday", "Thursday", "Friday", "Saturday" };
S / u / n / d / a / y / \0 / ? / ? / ?
M / o / n / d / a / y / \0 / ? / ? / ?
T / u / e / s / d / a / y / \0 / ? / ?
W / e / d / n / e / s / d / a / y / \0
T / h / u / r / s / d / a / y / \0 / ?
F / r / i / d / a / y / \0 / ? / ? / ?
S / a / t / u / r / d / a / y / \0 / ?
// Using a typedef for the day of the week */
typedefcharDayOfWeek[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. / char szWinnerFullName[15] = "Anita Break";
char szContestantFullName[15] = "Bob Wire";
A / n / i / t / a / B / r / e / a / k / \0 / ? / ? / ?
strcpy(szWinnerFullName, szContestantFullName);
After the strcpy, value of szWinnerFullName is
B / o / b / W / i / r / e / \0 / a / k / \0 / ? / ? / ?
char szName[10] = "Faye King";
char *pszVal = &szName[6];
0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9
F / a / y / e / K / i / n / g / \0
printf("szName=%s, szVal=%s\n", szName, pszVal);
Output:
szName=??, szVal=??
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 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.
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.
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 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. / 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
char szFullName[30] = "Anita ";
strcat(szFullName, "Goodgrade");
causes szFullName to contain "Anita Goodgrade".
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 iDelim;
char szInputpath[] = "
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");
That returns the address of szNursery[1]
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(). / Multiple independent uses of strtok can affect the results of each other.
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 non-printable 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.
Example: countCh("Mississippi", 's') returns 4 / int countCh(char szString[], char ch)
{
int i;
int iCount = 0;
??
}
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. / // There are many approaches. One is to use countCh
// on each of the individual characters in szMatchChars
int countAny(char szString[], char szMatchChars[])
{
int i; // Used to iterate through each of the
// characters in szMatchChars.
int iCount = 0;
// Iterate through the characters in szMatchChars
??
}
©2018 Larry W. Clark, UTSA CS students may make copies for their personal use