char arrays
string
data type (class)
char array
// String for storing a name char name[20];
// Make name contain Smith char name[20] = "Smith"; // WRONG! Can't assign a string after declaration name = "White";
// name has size of 6 char name[] = "Smith";
\0
char name[20] = "Bob";
name = B o b \0 ? ? ? ?
slots 0 1 2 3 4 5 6 ... 19
// WRONG! There's no slot available for \0 char name[5] = "Becky";
char name[20]; cout << "Name? "; cin >> name; cout << "Hello, " << name << "!";
Name? Bob Hello, Bob!
cout statement knows to stop outputting chars
when it hits the null terminator
cin statement stops adding chars to the char array
when it encounters white space (space, tab, carriage return)
char name[20]; cout << "Name? "; cin >> name; cout << "Hello, " << name << "!";
Name? Bob Smith Hello, Bob!
getline function to read all chars including space
char name[20]; cout << "Name? "; cin.getline(name, 20); cout << "Hello, " << name << "!";
Name? Bob Smith Hello, Bob Smith!
getline is the size of the array
getline is safer for reading input because if the user enters more
than 19 chars, only the first 19 chars will be placed in the array
[index]
char cheer[50] = "Let's go Bisons!"; cout << cheer[0]; // L cheer[9] = 'M'; // Changes B to M cout << cheer; // Let's go Misons!
char cheer[50] = "Let's go Bisons!";
// Count how many spaces are in the string
int spaces = 0;
for (int i = 0; cheer[i] != '\0'; i++)
if (cheer[i] == ' ')
spaces++;
cout << "Total spaces: " << spaces;
char cheer[50] = "Let's go Bisons!"; // Capitalize every letter for (int i = 0; cheer[i] != '\0'; i++) cheer[i] = toupper(cheer[i]); cout << cheer; // LET'S GO BISONS!
cstring contains over 20 functions, but we will cover only 4 here
strcpy_s(dest, src) - Replace the dest
string with the src string
char s[10] = "Goodbye"; strcpy_s(s, "Hello"); cout << s; // Hello
src can be a char array or a literal
src must be small enough to fit in the
dest string or your program will go outside the array
bounds and crash!
strcat_s(dest, src) - Append src onto the
back of dest
char s[10] = "Hey"; strcat_s(s, " there"); cout << s; // Hey there
src can be a char array or a literal
dest isn't large enough to hold
the appended string then your program will go beyond the array bounds
strlen(s) - Return the number of chars in s
before the null terminator
char s[10] = "Hey"; cout << strlen(s); // 3
s can be a char array or a literal
char s[] = "How are you?"; cout << strlen(s); // 12 s[3] = '\0'; // Replace space after "How" with null terminator cout << s; // How cout << strlen(s); // 3
strcmp function compares ASCII
values of chars in two arrays
strcmp(s1, s2) - Returns 0 if s1 == s2,
value < 0 if s1 < s2, or value > 0 if s1 > s3
char x[10] = "hello"; char y[10] = "help"; // 'l' and 'p' chars determine order int c = strcmp(x, y); if (c == 0) cout << "Strings are identical"; else if (c < 0) cout << x << " is before " << y; // hello is before help else cout << x << " is after " << y;
char x[10] = "hello"; char y[10] = "Help"; // 'h' and 'H' chars determine order int c = strcmp(x, y); if (c == 0) cout << "Strings are identical"; else if (c < 0) cout << x << " is before " << y; else cout << x << " is after " << y; // hello is after Help