Strings
Characters
Characters are assigned by an integer on an ASCII table. This means we can do arithmetic on characters.
char myChar = 'a';
myChar += ('z' - 'x'); // 'z' - 'x' = 2, and 'a' + x = 'c'
Strings
A string is just a char array in C
The following are equivalent:
char s[] = "Hello";
char t[] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Null char required
We can have Pointers to strings
char s[] = "Hello";
char *t = "Hello"; // This becomes an immutable constant
printf("%s\n", t); // Output "Hello"
printf("%zu\n", sizeof(s)); // Ouptut 6 (because + null char)
printf("%zu\n", sizeof(t)); // Ouptup 8 (because pointer)
String Utils
/**
* @returns the lengnth of s. Does not include null char
*/
size_t strlen(const char *s);
/**
* Copies s1 into s0
* s0 must have enough room including the null character, or this may overwrite
* bits we do not want it to touch
*
* @returns pointer to `s0`
*/
char *strcpy(char *s0, const char *s1);
=REMEMBER TO INCLUDE SPACE FOR NULL CHAR=
/**
* Copies the first `n` elements from s1 into s0.
* Will not add null character for you, DIY
*
* @returns pointer to `s0`
*/
char *strncpy(char *s0, const char *s1, size_t n);
/**
* Concatnates s1 onto s0.
* s0 must have enough room.
* A null character is added to the end automatically[w]()
*
* @returns pointer to `s0`
*/
char *strcat(char *s0, const char *s1);
/**
* Concatnates the first n characters of s1 onto s0.
* s0 must have enough room.
* A null character is added to the end automatically[]()
*
* @returns pointer to `s0`
*/
char *strncat(char *s0, const char *s1, size_t n);
/**
* Compares the two strings lexicographically (ie. comparing ASCII values)
*
* @returns < 0 if s0 < s1
* @returns > 0 if s0 > s1
* @returns 0 if s0 == 1
* @returns the index where the characters first differ
*/
int strcmp(const char *s0, const char *s1);