Deadlines/Announcements

๐Ÿค– Project 4 due Saturday, Feb 19th (9PM)

this weekโ€™s slides: bit.ly/cs31w22_week6

prior slides (arrays): bit.ly/cs31w22_week5

credit: nikki woo+zhehui+rosemary

Today's Agenda

C Strings ๐Ÿงต

A way to represent strings without using C++ string library Need some way to tell how big the cstring is --> add '\0' to end of cstring

#include <iostream>
#include <cstring> // contains functions to manipulate cstrings
using namespace std;

// need to have s[20] because need to account for zero byte at end
char s[20] = "Megan Thee Stallion";
					//  Megan Thee Stallion\\0
					//  0123456789012345678 9
					//            111111111 1

// convention for looping through a cstring, check that current char
// is not the zero byte at the end
for (int k = 0; s[k] != '\\0'; k++) {
	cout << s[k];
}

// for cstrings, you can also do this
cout << s << endl; // will write out string up to (not including) '\\0'

// how to get a user input and store into cstring
char user_input[100];
cin.getline(user_input, 99); // 99 because need to make room for nullbyte!

// to copy a cstring 
strcopy(user_input, s); // copy s INTO user_input

// get length of cstring
int len = strlen(s); // will return 19 --> count of chars not including '\\0'

// concaternate cstrings
strcat(s, " :)"); 
cout << s << endl; // Megan Thee Stallion :)

// compare cstrings (can't use <, >)
int result = strcmp(s, user_input);
// result negative if s < user_input
// result = 0 if s == user_input
// result positive if s > user_input

Project 4 Functions

General Guidelines

<aside> ๐Ÿšซ No extra array allocations should be necessary. So use loops and do it a different way.

</aside>