Allows you to store multiple values of the same type back-to-back (contiguously), with the same name.
Example:
int scores[3];
[1] = 100;
scores[2] = 75;
scores[3] = 50; scores
Strings are arrays of char
’s!
How does the computer know where one string ends and begins in memory?
Answer: \0
, or 00000000
bits serve as delimiters.
"HI!"
is actually stored with 4 bytes represented in ASCII as decimal as 72 73 33 0
Example: Using \0
to find when a string ends (determine length of string)
int string_length(string x)
{
int length = 0;
while(x[i] != '\0')
{
++;
length}
return length;
}
strlen()
function in the #include <string.h>
library instead of re-inventing the wheelprintc
Suppose we have a string we want to print using printc
.
We would have to iterate over every char in the string and print it out using a loop, like so:
void print_string(string x)
{
// Syntactic Sugar: Storing strlen in a var instead of re-running it every loop
for (int i = 0; length = strlen(x); i < length; i++)
{
("%c", s[i]);
printc}
}
Resource: https://asciichart.com
(string s)
string uppercase_string{
for (int i = 0; length = strlen(x); i < length; i++)
{
// Detect lowercase ASCII (96 <= s[i] <= 122)
if (s[i] >= 'a' && s[i] <= 'z')
{
[i] -= 32;
s}
("%c", s[i]);
printc}
}
islower()
function in ctypes.h
instead of s[i] >= 'a' && s[i] <= 'z'
; or just the toupper()
function in ctypes.h