functions include: Conversion between upper and lower case Test for upper or lower case Test for letters (and digits and alphanumeric) Test for blank spaces
uppercase letter (A-Z), converts to lowercase (a-z) Otherwise, returns the same character int toupper(int) If lowercase letter (a-z), converts to uppercase (A-Z) Otherwise, returns the same character
lowercase letter (a-z), returns a non-zero integer Returns 0 if is not lowercase letter int isupper(int) If uppercase letter (A-Z), returns a non-zero integer Returns 0 if not an uppercase letter
letter (a-z, A-Z), returns non-zero, otherwise zero int isdigit(char) If digit (0-9), returns non-zero, otherwise zero int isalnum(int) Is letter or digit (0-9, a-z, A-Z), returns non-zero, otherwise zero
it is a punctuation character, returns non-zero If alphanumeric, space, tab, or newline character, returns zero int isspace(int) If space, tab, or newline, returns non-zero Otherwise, returns zero
the output of the character functions from the past few slides Reminder: the newline (pressing the “Enter” key, written in our programs as '\n') is also a character!! % cp charfunctions.c program.c % make -f makefile-program % ./program
string is an array of characters which ends with the end of string character, called the null character ('\0'), which has the numerical value of zero (0) You can initialize strings in several ways, but you always need to include the null character ('\0') at the end of the string
to initialize strings Shows content of strings, including null character ('\0') Shows what NOT to do with strings, which is write over the null character % cp string.c program.c % make -f makefile-program % ./program
NOT any addresses char array3[2][5]={‘z’,‘e’,‘r’,‘o’, ‘\0’,‘o’,‘n’,‘e’,‘\0’,‘\0’}; printf(“%s”,array2[0]); \\zero printf(“%c”,array2[0][0]); \\z array2[0][0]='A';//change character array2[0]=array2[1]; //incompatible
to be careful that the user does not enter too many characters 1. If the number of characters is greater than the length of our string, it can write over other parts of memory and/or crash our program 2.Also, we need to think about the input stream: Do we want to leave extra characters on input stream or remove the extra character from the input stream?
end of file (EOF), a newline ('\n'), blank space (' '), or tab ('\t') If the user enters too many characters, these are left on the input stream Make sure the size of the string is correct, otherwise your program may crash and/or overwrite memory
end of file (EOF) or a newline ('\n'), so it reads one line of input If the user enters too many characters, these are taken off the input stream Make sure the size of the string is correct, otherwise your program may crash and/or overwrite memory
*str2, size_t n); Copies up to n characters from the string str2 to str1 Copying stops when n characters are copied or when reaches the null character in str2 Returns address of str1 strncpy(string1, string2, MAX);
*str2, size_t n); Appends string str2 to end of string str1 up to n characters Copying stops after n characters or null character Returns str1 strncat(string1, string2, MAX);
char *str2); Compares the string str1 to the string str2 Returns zero if str1 and str2 are equal Returns negative integer if str1 < str2 Returns positive integer if str1 > str2 int result = strcmp(string1, string2);
*str, int c); Searches for the first occurrence of the character c in the string str Returns a pointer pointing to the first matching character, or null if no match was found char *pointer = strchr(string, character);
*str1, const char *str2); Finds the first occurrence of the entire string str2 which appears in the string str1 Returns a pointer to the first occurrence of str2 in str1, or null if no match was found char *pointer = strstr(string1, string2);
*str2); Used to break a string into tokens, a sequence of characters separated by delimiting characters (usually spaces or punctuation marks) str1 is the string to be tokenized str2 is the set of characters separating the tokens
argument Following function calls: have NULL as first argument Returns a pointer to current token If no more tokens are found, returns a null pointer char *pointer = strtok(string, delimiter); pointer = strtok(NULL, delimiter);