Manipulating A C - STRINGS AS AN ARRAY OF CHARACTERS Flashcards
(40 cards)
testing the value of a character
functions test a single char argument for a specific quality and return true or false
isupper
function in the header that tests to see if a character is an uppercase letter
isalpha
function in the header that tests to see if a character is a letter of the alphabet
isalnum
function in the header that tests to see if a character is a letter of the alphabet or a digit
isdigit
function in the header that tests to see if a character is a digit from 0 to 9
islower
function in the header that tests to see if a character is a lowercase letter
isprint
function in the header that tests to see if a character is a PRINTABLE CHARACTER including a space (basically as far as I can tell, not the null terminator)
ispunct
function in the header that tests to see if a character is a PRINTABLE CHARACTER other than a digit, letter, or space
isspace
function in the header that tests to see if a character is a whitespace characterspace ‘ ‘newline \nvertical tab \vtab \t
toupper
function in the header that returns the uppercase equivalent of a char-type argument (w/o changing the original as per C++ usual). Will return an uppercase letter or nonletter argument unchangedcout «_space;toupper(‘a’) for instance
tolower
function in the header that returns the lowercase equivalent of a char-type argument (w/o changing the original as per C++ usual). Will return a lowercase letter or nonletter argument unchangedcout «_space;tolower(‘A’) for instance
string
generic term that describes any consecutive sequence of characters
C-string
string whose characters are stored in consecutive memory locations and are followed by the null terminator (ASCII 0). Basically a char array with space for each character, +1 for the null terminatorString literals/constants are stored as C-strings
implicit sizing - char array
“since a string literal is itself read as an array, and the computer notices the number of characters, you can initialize an array with a string literalchar characterarray[] = ““Grace”” which will have 6 spotsand those spots will be filled byG r a c e \0but you can write over them”
c-string console input
“char characterarray[21];cin»_space; characterarray;in the second line, ““characterarray”” is a variable that represents the array with the pointer at the beginning of itcin will write past the end of the array if you cin more than 21 characters. I think this is called overloading. in any case, cin.getline()”
cin.getline() for c-strings and character arrays
cin.getline(stordes, MAXLN);and then you type stuff and hit ENTER when you’re donestordes is the storage destination. Because it’s a string literal, it’ll be stored in a character array. The name of the array by itself represents the array with the pointer reset to index at 0.maxln (constant integer) is the maximum length of the input, so usually the size of the array. Because the input includes the null terminator, an 80-index array will intake 79 values. If the input isn’t that long, it’ll append the null terminator at the end of the input.it does NOT truncate the array to the size of the string
strlen
“function in the header that returns the length of the c-string argument. NOT counting the null terminator nor the size of the arrayargument can be name of the array, pointer variable (later), or the string literalbecause the argument is the name of the array (not the array w/indexed size), the pointer is set to the beginning so it doesn’t know the size of the array after the null terminatorlength = strlen(arrayname) or =strlen(““Grace””)”
sizeof
I’m sure I wrote this down in a previous chapter but this counts the value spots in an array (different than strlen!)
strcat
function in the header that concatenates one string to another. Argument is two C-strings (using their array names sans indices). Be sure to put a space in one of the string literals, because strcat won’t insert it and there’s no space to add it as an argumentcopies the 2nd string over to the end of the 1st. Leaves the 2nd string (saved under its own name) alone, but 1st is changedstrcat(string1, string2);this can overload the bounds of an array
strncat
function in the header that works like strcat but there’s a third argument specifying the max number of characters from 2nd string to append to 1st stringstrncat(string1, string2, 10);
strcpy
function in the header that writes one string over another without a loop (or copies a string to an array). I assume this would be used to take one string, multiply it or something, then strcpy it to a new column in a structure or somethingstrcpy(string1, string2);this can overload the bounds of an array
strncpy
function in the header that works like strcpy but there’s a third argument specifying the max number of characters from 2nd string to copy to the 1ststrncpy(string1, string2, 5);*come back to this
strstr
“function in the header that searches for a string inside of a string. First argument is the string to be searched and the second is what you’re looking for. It’ll search a c-string character array and return address of the first character of the second stringstring1 = ““blood types A B AB O”“strstr(string1, ““AB”“)I don’t know what would happen if I searched just for A or Bbut apparently it returns the rest of string 1 after starting at the beginning of string 2”
strcmp
“function in the header (?!?! maybe this is a typo) that compares c-strings, since relational operators won’t workstrcmp(string1, string2)This function will return 0 if the strings are equal on a character-by character basis,-1 if string1 comes BEFORE string2 in alphabetical order1 if string1 comes AFTER string2 in alphabetical orderthis works well with if statements, and you can use the returns with relational operatorsusing ! - the computer sees 0 as false. If your strings are equal, and you want the computer to report ““true”” for that (just because it’s more intuitive) put ! in front of the callif (!strcmp(string1, string2))I don’t think that works if the strings aren’t equal but I’ll test it”