Chartacter, C-strings(10.1-10.5)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/36

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 10:22 PM on 10/10/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

37 Terms

1
New cards
  • <cctype>


  • Header file you must include to use the character-testing functions (isalpha, isdigit, etc.).


2
New cards
  • Return value of character-testing functions


  • Technically an int. Nonzero means true, 0 means false.


3
New cards
  • isalpha / isdigit / isalnum


  • isalpha is true for a letter. isdigit is true for 0 to 9. isalnum is true for a letter or a digit.


4
New cards
  • islower / isupper


  • islower is true for a lowercase letter. isupper is true for an uppercase letter.


5
New cards
  • isspace


  • True for whitespace: space, newline (\n), tab (\t), vertical tab (\v).



6
New cards
  • ispunct vs isprint



  • ispunct is true for a printable character that is NOT a letter, digit, or space. isprint is true for any printable character, including space.


7
New cards
  • Loop condition count < size - 1


  • Stops before the last array element, which holds the null terminator '\0'.


8
New cards
C-string
Characters stored in consecutive memory and ended by a null terminator '\0'.
9
New cards
Null terminator
'\0', the byte with ASCII code 0. It marks the end of a C-string.
10
New cards
String literal storage
Every string literal is stored as a C-string with '\0' added. "Bailey" uses 7 bytes.
11
New cards
C-string array size
Characters needed plus 1 for '\0'. char name[21] holds at most 20 characters.
12
New cards
char name[] = "Jasmine";
Creates an array with exactly 8 elements: 7 letters plus '\0'.
13
New cards
cin >> name (C-string)
Reads one word and stops at whitespace. No size limit, so it can overflow the array.
14
New cards
cin.getline(line, SIZE)
Reads a whole line, including spaces. Reads at most SIZE - 1 characters, then adds '\0'.
15
New cards
Loop through a C-string
while (s[i] != '\0'). Stop at the null terminator, not at the array size.
16
New cards
Why learn C-strings
Legacy C++ code, library functions that need them, and C libraries all use C-strings.
17
New cards
toupper(ch)
Returns the uppercase equivalent of ch. Non-letters and uppercase letters are returned unchanged.
18
New cards
tolower(ch)
Returns the lowercase equivalent of ch. Non-letters and lowercase letters are returned unchanged.
19
New cards
Does toupper change its argument?
No. It only returns the converted value. You must store the result to keep it.
20
New cards
Case-insensitive input check
Convert with toupper or tolower, then compare once. Example: toupper(goAgain) != 'Y'.
21
New cards
atoi / atol / atof
Convert a C-string to int, long, or double. Need . Bad input is undefined.
22
New cards
stoi / stol / stod (and similar)
Convert a string or C-string to int, long, or double. Need .
23
New cards
stoi with invalid or too large input
Invalid input throws invalid_argument. A value out of range throws out_of_range.
24
New cards
to_string(value)
Converts a number to a string object. Needs .
25
New cards
Newline problem
cin >> leaves the newline in the buffer, so the next getline reads an empty line.
26
New cards
Two fixes for the newline problem
Call cin.ignore() before getline, or read all input with getline and convert with stoi or stod.
27
New cards
Average with integers
Use static_cast(total) / count to avoid integer division.
28
New cards
Header for C-string functions
#include
29
New cards
strlen(s)
Returns the number of characters before the null terminator. Not the array size.
30
New cards
strcat(s1, s2)
Appends s2 to the end of s1. s1 changes, s2 does not. Adds no space. s1 must be big enough.
31
New cards
strcpy(dest, src)
Copies src into dest, including the null terminator. Overwrites dest. No bounds checking.
32
New cards
strncat(s1, s2, n)
Like strcat but appends at most n characters from s2. Safer.
33
New cards
strncpy(s1, s2, n)
Like strcpy but copies at most n characters. If n is not greater than the length of s2, no null terminator is added, so add it yourself.
34
New cards
strstr(s1, s2)
Returns the address of the first s2 inside s1, or nullptr if not found.
35
New cards
strcmp(s1, s2)
Returns 0 if equal, negative if s1 comes before s2, positive if s1 comes after s2. Case sensitive.
36
New cards
Why not use == to compare C-strings?
C-strings are arrays. == compares addresses, not characters. Use strcmp.
37
New cards
!strcmp(a, b)
True when the two strings are equal.