SyntaxPilot
Courses/C Coding Questions/Lesson 18
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

C Coding Questions · Lesson 18 of 30

String questions IV

write your own strcpystrcmp and strcat; string to integer (atoi) with signspacesinvalid input and overflow

These are the notes for C Coding Questions, episode 18, placement practice for people who finished the SyntaxPilot C course. String questions, part four: write the library yourself. Your own strcpy and strcat, your own strcmp, and string to integer (atoi) with spaces, a sign, invalid input and overflow.

Each question follows the same steps: read the problem, think out loud, the obvious attempt, a real failure, why, the fix, every test case, and the complexity.

Every program was compiled with gcc -Wall -O2 -Wno-unused-result main.c -o main -lm (gcc 13.3, Linux x86-64, 32-bit int, 64-bit long long), the SyntaxPilot compiler's C command with warnings on, and every output below is real. In the input lines below, ⏎ means pressing Enter. Timings are from our machine.

Real-life pictures

In real life In C
repainting a station board from "Mumbai Central" to "Pune": paint over it, then mark where the new name ends strcpy: copy the chars, then the '\0'
adding a coach to a train: walk to the last coach first strcat (concatenate = join): find the '\0', copy there
dictionary order: the first letter that differs decides strcmp: negative, 0 or positive
a bank clerk reads the amount on a form: skip blanks, note a minus, read digits, stop atoi (ASCII to integer)

Q1: Your own strcpy and strcat

Problem. Write my_strcpy(dst, src), which copies the text of src into dst, and my_strcat(dst, src), which adds src at the end of dst, without <string.h>.

Think out loud.

  • dst holds "Mumbai Central"; copy "Pune" → "Pune"; then add " Junction" → "Pune Junction".
  • Edge cases: the old text is longer than the new one; an empty src; dst must be big enough for the result plus '\0' (neither function checks).

The obvious attempt: dst = src

#include <stdio.h>

int main() {
    char src[20] = "Pune";
    char dst[20] = "Mumbai Central";
    dst = src;
    printf("%s\n", dst);
    return 0;
}

Compiler output:

main.c: In function 'main':
main.c:6:9: error: assignment to expression with array type
    6 |     dst = src;
      |         ^

Why. An array's name stands for its fixed place in memory. You can change the elements, but an array as a whole can't be assigned with =. (A struct containing an array can be assigned; an array can't.)

Second attempt: copy the chars, forget the '\0'

#include <stdio.h>

void my_strcpy(char dst[], const char src[]) {
    int i = 0;
    while (src[i] != '\0') {
        dst[i] = src[i];
        i++;
    }
}

int main() {
    char dst[20] = "Mumbai Central";
    my_strcpy(dst, "Pune");
    printf("\"%s\"\n", dst);
    return 0;
}

Output:

"Puneai Central"

Why. The loop stops at src's '\0' without copying it, so dst still ends where Mumbai Central ended: P u n e + a i ␣ C e n t r a l.

And a + b for strcat?

#include <stdio.h>

int main() {
    char a[20] = "Pune";
    char b[20] = " Junction";
    printf("%s\n", a + b);
    return 0;
}

Compiler output:

main.c: In function 'main':
main.c:6:22: error: invalid operands to binary + (have 'char *' and 'char *')
    6 |     printf("%s\n", a + b);
      |                    ~ ^
      |                    | |
      |                    | char *
      |                    char *

Why. In an expression an array turns into the address of its first element (a char *, "pointer to char"); adding two addresses means nothing, and C has no + for strings.

The fix: copy the '\0' too; strcat walks to the end first

#include <stdio.h>

void my_strcpy(char dst[], const char src[]) {
    int i = 0;
    while (src[i] != '\0') {
        dst[i] = src[i];
        i++;
    }
    dst[i] = '\0';
}

void my_strcat(char dst[], const char src[]) {
    int i = 0, j = 0;
    while (dst[i] != '\0') i++;
    while (src[j] != '\0') dst[i++] = src[j++];
    dst[i] = '\0';
}

int main() {
    char dst[20] = "Mumbai Central";
    my_strcpy(dst, "Pune");
    printf("\"%s\"\n", dst);
    my_strcat(dst, " Junction");
    printf("\"%s\"\n", dst);
    my_strcpy(dst, "");
    printf("\"%s\"\n", dst);
    return 0;
}

Output:

"Pune"
"Pune Junction"
""

Time O(n) each (n = the characters walked: for strcat, the old length plus the added one). Space O(1).

A shorter, classic form of the copy loop copies the '\0' as part of the loop, because the assignment's value is the char just copied:

#include <stdio.h>

void my_strcpy(char dst[], const char src[]) {
    int i = 0;
    while ((dst[i] = src[i]) != '\0')
        i++;
}

int main() {
    char dst[20] = "Mumbai Central";
    my_strcpy(dst, "Pune");
    printf("\"%s\"\n", dst);
    return 0;
}
"Pune"

The strcat-in-a-loop trap: O(n²)

Building a long string by calling strcat again and again walks from the start to the '\0' every time: 1 + 2 + … + n ≈ n²/2 steps. Remember the length instead:

#include <stdio.h>
#include <time.h>

#define N 100000
char a[N + 1], b[N + 1];

double secs(clock_t start) {
    return (double)(clock() - start) / CLOCKS_PER_SEC;
}

void my_strcat(char dst[], const char src[]) {
    int i = 0, j = 0;
    while (dst[i] != '\0') i++;
    while (src[j] != '\0') dst[i++] = src[j++];
    dst[i] = '\0';
}

int cat_at(char dst[], int len, const char src[]) {
    int j = 0;
    while (src[j] != '\0') dst[len++] = src[j++];
    dst[len] = '\0';
    return len;
}

int main() {
    clock_t t = clock();
    for (int k = 0; k < N; k++) my_strcat(a, "x");
    printf("strcat each time: %d chars, %.2f s\n", N, secs(t));
    t = clock();
    int len = 0;
    for (int k = 0; k < N; k++) len = cat_at(b, len, "x");
    printf("keep the length:  %d chars, %.2f s\n", len, secs(t));
    return 0;
}

Output (timings from our machine):

strcat each time: 100000 chars, 1.48 s
keep the length:  100000 chars, 0.00 s

Q2: Your own strcmp

Problem. Write my_strcmp(a, b): negative if a comes first in dictionary (character-code) order, 0 if equal, positive if b comes first. Only the sign matters.

Think out loud.

  • chai chai → 0; apple banana → negative; app apple → negative (a prefix comes first); apple app → positive.
  • Zebra apple → negative: 'Z' is 90 and 'a' is 97, so every capital comes before every small letter.
  • Edge cases: equal strings, one a prefix of the other, capitals.

The obvious attempt: a == b

#include <stdio.h>

int main() {
    char a[20] = "chai", b[20] = "chai";
    if (a == b) printf("same\n");
    else printf("different\n");
    return 0;
}

Compiler output:

main.c: In function 'main':
main.c:5:11: warning: comparison between two arrays [-Warray-compare]
    5 |     if (a == b) printf("same\n");
      |           ^~
main.c:5:11: note: use '&a[0] == &b[0]' to compare the addresses

Output:

different

Why. a == b compares the two addresses, and two different arrays never live at the same address. gcc 12+ warns about it in -Wall (-Warray-compare).

Second attempt: stop when either word ends

#include <stdio.h>

int my_strcmp(const char a[], const char b[]) {
    for (int i = 0; a[i] != '\0' && b[i] != '\0'; i++)
        if (a[i] != b[i]) return a[i] - b[i];
    return 0;
}

int main() {
    char a[50], b[50];
    while (scanf("%49s %49s", a, b) == 2)
        printf("%s %s: %d\n", a, b, my_strcmp(a, b));
    return 0;
}

Input chai chai⏎ apple banana⏎ Zebra apple⏎ app apple⏎ apple app⏎:

chai chai: 0
apple banana: -1
Zebra apple: -7
app apple: 0
apple app: 0

Why. After app the shorter word reaches '\0', the loop stops, no difference was seen, so it returns 0. But the end of a word is a difference: '\0' (0) vs 'l' (108).

The fix: move on while equal and not at the end

If a[i] == b[i] and a[i] isn't '\0', then b[i] isn't either, so checking a alone is enough. Then return the difference at the first position where they differ (or both are '\0': 0). The real strcmp compares as unsigned char, so we do too.

#include <stdio.h>

int my_strcmp(const char a[], const char b[]) {
    int i = 0;
    while (a[i] != '\0' && a[i] == b[i]) i++;
    return (unsigned char)a[i] - (unsigned char)b[i];
}

int main() {
    char a[50], b[50];
    while (scanf("%49s %49s", a, b) == 2)
        printf("%s %s: %d\n", a, b, my_strcmp(a, b));
    return 0;
}

Same input:

chai chai: 0
apple banana: -1
Zebra apple: -7
app apple: -108
apple app: 108

Time O(n), n = the shorter length (it stops at the first difference). Space O(1). Never test strcmp(a, b) == -1: the standard only promises the sign, and real implementations return other values.

Q3: String to integer (atoi)

Problem. Turn a string into an int the way atoi does for valid input, made safe:

  1. skip leading whitespace;
  2. read one optional + or -;
  3. read digits until the first non-digit;
  4. no digits → 0; a value too big for int → clamp (hold it) at INT_MAX or INT_MIN.

Think out loud.

  • "42" → 42, " -42" → −42, "+7" → 7, "4193 rupees" → 4193, "rupees 987" → 0, "-" → 0.
  • "2147483647" → 2147483647 (INT_MAX), "2147483648" → 2147483647 (clamped), "-2147483648" → −2147483648 (INT_MIN, exactly), "99999999999" → 2147483647.
  • A digit's value is c - '0': '7' - '0' = 7.

The obvious attempt: every char is a digit

#include <stdio.h>
#include <string.h>

int my_atoi(const char s[]) {
    int n = 0;
    for (int i = 0; s[i] != '\0'; i++)
        n = n * 10 + (s[i] - '0');
    return n;
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        printf("\"%s\" -> %d\n", s, my_atoi(s));
    }
    return 0;
}

Input 42⏎ ␣␣␣-42⏎ +7⏎ -⏎:

"42" -> 42
"   -42" -> -1776258
"+7" -> -43
"-" -> -3

Why. c - '0' is a digit only when c is '0'..'9'. '+' is 43: 43 − 48 = −5, then −5 × 10 + 7 = −43. Spaces (32 − 48 = −16) and '-' (−3) get mixed in the same way. (Only these four lines are used here: longer inputs like 4193 rupees would make n overflow, which is undefined behaviour, see below.)

Second attempt: spaces, sign, digits (but int overflows)

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int my_atoi(const char s[]) {
    int i = 0, sign = 1, n = 0;
    while (isspace((unsigned char)s[i])) i++;
    if (s[i] == '+' || s[i] == '-') {
        if (s[i] == '-') sign = -1;
        i++;
    }
    while (isdigit((unsigned char)s[i])) {
        n = n * 10 + (s[i] - '0');
        i++;
    }
    return sign * n;
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        printf("\"%s\" -> %d\n", s, my_atoi(s));
    }
    return 0;
}

Input 42⏎ ␣␣␣-42⏎ +7⏎ 4193 rupees⏎ rupees 987⏎ -⏎:

"42" -> 42
"   -42" -> -42
"+7" -> 7
"4193 rupees" -> 4193
"rupees 987" -> 0
"-" -> 0

All right. Now the big ones, 2147483647⏎ 2147483648⏎ -2147483648⏎ 99999999999⏎:

"2147483647" -> 2147483647
"2147483648" -> -2147483648
"-2147483648" -> -2147483648
"99999999999" -> 1215752191

Why. INT_MAX is 2147483647 and INT_MIN is −2147483648 (a 4-byte int; both in <limits.h>). 214748364 × 10 + 8 doesn't fit in an int: signed integer overflow, which in C is undefined behaviour. Here it happened to wrap around to a negative number; the standard promises nothing, and an optimising compiler may do something else entirely. -2147483648 only looked right: it also overflowed (2147483648 doesn't fit before the sign is applied).

The fix: build in a long long, clamp before it overflows

long long holds at least ±9.2 × 10¹⁸. After each digit, if sign * n has gone past INT_MAX return INT_MAX; below INT_MIN, return INT_MIN. We stop at once, so n stays below about 2.2 × 10¹⁰ and the long long itself can never overflow.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

int my_atoi(const char s[]) {
    int i = 0, sign = 1;
    long long n = 0;
    while (isspace((unsigned char)s[i])) i++;
    if (s[i] == '+' || s[i] == '-') {
        if (s[i] == '-') sign = -1;
        i++;
    }
    while (isdigit((unsigned char)s[i])) {
        n = n * 10 + (s[i] - '0');
        if (sign * n > INT_MAX) return INT_MAX;
        if (sign * n < INT_MIN) return INT_MIN;
        i++;
    }
    return (int)(sign * n);
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        printf("\"%s\" -> %d\n", s, my_atoi(s));
    }
    return 0;
}

The big ones:

"2147483647" -> 2147483647
"2147483648" -> 2147483647
"-2147483648" -> -2147483648
"99999999999" -> 2147483647

And the small ones again:

"42" -> 42
"   -42" -> -42
"+7" -> 7
"4193 rupees" -> 4193
"rupees 987" -> 0
"-" -> 0

Time O(n), space O(1).

Without long long

If long long isn't allowed, check before multiplying: n > (INT_MAX - d) / 10 means n * 10 + d would overflow. Building the number as a negative value handles INT_MIN too, because INT_MIN has no positive partner:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

int my_atoi(const char s[]) {
    int i = 0, neg = 0, n = 0;   /* n is built as a negative number */
    while (isspace((unsigned char)s[i])) i++;
    if (s[i] == '+' || s[i] == '-') {
        neg = (s[i] == '-');
        i++;
    }
    while (isdigit((unsigned char)s[i])) {
        int d = s[i] - '0';
        if (n < (INT_MIN + d) / 10) return neg ? INT_MIN : INT_MAX;
        n = n * 10 - d;
        i++;
    }
    if (!neg && n == INT_MIN) return INT_MAX;
    return neg ? n : -n;
}

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        printf("\"%s\" -> %d\n", s, my_atoi(s));
    }
    return 0;
}

Input 42⏎ ␣␣␣-42⏎ 2147483647⏎ 2147483648⏎ -2147483648⏎ -2147483649⏎ 99999999999⏎:

"42" -> 42
"   -42" -> -42
"2147483647" -> 2147483647
"2147483648" -> 2147483647
"-2147483648" -> -2147483648
"-2147483649" -> -2147483648
"99999999999" -> 2147483647

The real atoi, and strtol

The library atoi (in <stdlib.h>) behaves the same way for valid input, but on overflow its behaviour is undefined, and for bad input it just returns 0, so "0" and "rupees" look the same. In real code use strtol (string to long): it gives back a pointer end to where it stopped, and sets errno to ERANGE on overflow.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main() {
    char s[100];
    while (fgets(s, sizeof s, stdin) != NULL) {
        s[strcspn(s, "\n")] = '\0';
        char *end;
        errno = 0;
        long v = strtol(s, &end, 10);
        if (end == s) printf("\"%s\": not a number\n", s);
        else if (errno == ERANGE) printf("\"%s\": out of range\n", s);
        else printf("\"%s\": %ld, stopped at \"%s\"\n", s, v, end);
    }
    return 0;
}

Input 0⏎ rupees⏎ 4193 rupees⏎ 99999999999999999999⏎:

"0": 0, stopped at ""
"rupees": not a number
"4193 rupees": 4193, stopped at " rupees"
"99999999999999999999": out of range

(long is 64-bit here, so 99999999999 alone would fit; twenty nines do not.)

Complexity summary

Task Method Time Space
copy dst = src compile error
loop without '\0' O(n), old tail stays O(1)
loop + '\0' O(n) O(1)
join a + b compile error
walk to '\0', copy there O(len(dst) + len(src)) O(1)
strcat k times in a loop O(n²) O(1)
keep the length O(n) O(1)
compare a == b compares addresses
stop when either ends O(n), prefix = equal O(1)
while equal and not '\0' O(n), n = shorter length O(1)
atoi every char a digit O(n), wrong O(1)
spaces, sign, digits in an int O(n), overflow (UB) O(1)
long long + clamp O(n) O(1)

Common mistakes (and the real results)

Mistake What you see Why Fix
dst = src; error: assignment to expression with array type arrays can't be assigned copy char by char
copy loop without '\0' no error: "Puneai Central" the old end is still there dst[i] = '\0';
dst too small no error; writes past the end (undefined behaviour) strcpy/strcat never check size dst for src + '\0', or a safe copy (your turn)
a + b error: invalid operands to binary + (have 'char *' and 'char *') no + for strings my_strcat(a, b)
strcat in a loop no error: seconds instead of 0.00 s walks to the end each time keep the length
if (a == b) warning: comparison between two arrays [-Warray-compare], then different compares addresses my_strcmp(a, b) == 0
stop when either word ends no error: app apple: 0 the end is a difference while (a[i] != '\0' && a[i] == b[i])
strcmp(a, b) == -1 sometimes false for a < b (we got −108) only the sign is promised strcmp(a, b) < 0
n = n * 10 + (c - '0') for every char no error: "+7" -> -43 not every char is a digit isspace, sign, isdigit
building n in an int no error: 2147483648 -> -2147483648 (undefined behaviour) signed overflow long long + clamp, or check before multiplying

Your turn: a safe copy (solution)

Problem. copy_safe(dst, size, src) copies at most size − 1 characters, always ends dst with '\0', and returns 1 if all of src fit, 0 if it was cut. char d[5]; copy_safe(d, 5, "Mumbai") → "Mumb", returns 0.

Idea. Q1's copy loop with a second condition i < size - 1, then '\0' at i. It fit exactly when the loop stopped at src's end. size must be at least 1. O(n) time, O(1) space. (The library's strncpy is not this: it doesn't always add '\0'. snprintf(dst, size, "%s", src) is the standard safe copy.)

Show the solution
#include <stdio.h>

int copy_safe(char dst[], int size, const char src[]) {
    int i = 0;
    while (i < size - 1 && src[i] != '\0') {
        dst[i] = src[i];
        i++;
    }
    dst[i] = '\0';
    return src[i] == '\0';
}

int main() {
    char d[5];
    int ok = copy_safe(d, 5, "Mumbai");
    printf("\"%s\" %d\n", d, ok);
    ok = copy_safe(d, 5, "Pune");
    printf("\"%s\" %d\n", d, ok);
    ok = copy_safe(d, 5, "");
    printf("\"%s\" %d\n", d, ok);
    ok = copy_safe(d, 1, "Goa");
    printf("\"%s\" %d\n", d, ok);
    return 0;
}

Output:

"Mumb" 0
"Pune" 1
"" 1
"" 0

"Pune" is 4 letters + '\0': exactly 5, so it fits.

Practice questions (with solutions)

1. Your own strlen, strchr and strrev.

Solution
#include <stdio.h>

int my_strlen(const char s[]) {
    int n = 0;
    while (s[n] != '\0') n++;
    return n;
}

int my_strchr(const char s[], char c) {   /* index of the first c, or -1 */
    for (int i = 0; s[i] != '\0'; i++)
        if (s[i] == c) return i;
    return -1;
}

void my_strrev(char s[]) {
    int i = 0, j = my_strlen(s) - 1;
    while (i < j) {
        char t = s[i];
        s[i++] = s[j];
        s[j--] = t;
    }
}

int main() {
    char s[] = "SyntaxPilot";
    printf("%d %d %d\n", my_strlen(s), my_strchr(s, 'P'), my_strchr(s, 'z'));
    my_strrev(s);
    printf("%s\n", s);
    return 0;
}
11 6 -1
toliPxatnyS

2. Case-insensitive strcmp. Compare tolower of both characters.

Solution
#include <stdio.h>
#include <ctype.h>

int strcmp_ci(const char a[], const char b[]) {
    int i = 0;
    while (a[i] != '\0' &&
           tolower((unsigned char)a[i]) == tolower((unsigned char)b[i]))
        i++;
    return tolower((unsigned char)a[i]) - tolower((unsigned char)b[i]);
}

int main() {
    printf("%d %d %d\n", strcmp_ci("Zebra", "apple") > 0,
           strcmp_ci("CHAI", "chai") == 0, strcmp_ci("App", "apple") < 0);
    return 0;
}
1 1 1

(1 1 1: Zebra now comes after apple, CHAI equals chai, App comes before apple.)

3. Use strtol to tell "0" from "rupees". See the strtol program above: end == s means no digits were read.

Quick quiz

#include <stdio.h>
#include <string.h>

int main() {
    if (strcmp("Zebra", "apple") < 0)
        printf("Zebra first\n");
    else
        printf("apple first\n");
    return 0;
}

(A) Zebra first (B) apple first (C) a compile error (D) nothing

Answer

(A) Zebra first. strcmp compares character codes: 'Z' is 90, 'a' is 97. Every capital comes before every small letter in ASCII. For a real dictionary order, compare tolower of each character (practice 2).

Zebra first

Interview questions

  1. Write strcpy. Copy until '\0', and copy the '\0': while ((d[i] = s[i]) != '\0') i++;.
  2. Why can't you write a = b for arrays? An array isn't assignable; copy the elements.
  3. What does strcpy do if dst is too small? Writes past the end: undefined behaviour. Use snprintf or a size-limited copy.
  4. Why is strcat in a loop O(n²)? Each call walks to the end first; keep the length or a pointer to the end.
  5. What does strcmp return? Negative, 0 or positive; only the sign is guaranteed.
  6. Why is "app" less than "apple"? At index 3, '\0' (0) < 'l' (108).
  7. Why does a == b fail for strings? It compares addresses, not text.
  8. Implement atoi. Skip spaces, one sign, digits; stop at a non-digit; handle overflow by clamping.
  9. How do you detect overflow without a bigger type? Check n > (INT_MAX - d) / 10 before n = n * 10 + d (or build negative and compare with INT_MIN).
  10. Why is signed overflow dangerous? It is undefined behaviour; the compiler may assume it never happens.
  11. atoi vs strtol? strtol reports where it stopped and overflow (errno = ERANGE); atoi can't.

Glossary

Term Meaning Example
strcpy Copies a string, '\0' included, into another array strcpy(dst, "Pune")
strcat Concatenate: adds a string at the end of another "Pune" + " Junction"
Concatenate Join one after the other ab + cd = abcd
strcmp Compares two strings: negative, 0 or positive strcmp("app", "apple") < 0
Dictionary (lexicographic) order Ordered by the first character that differs apple < banana
Prefix The start of a longer string app is a prefix of apple
atoi ASCII to integer, in <stdlib.h> atoi("42") = 42
strtol String to long, reports where it stopped and overflow strtol(s, &end, 10)
Array type An array can't be assigned with = error: assignment to expression with array type
Address The place in memory where something is stored what a == b compares
char * Pointer to char: the address of a character an array name in an expression
const parameter A promise that the function won't change it const char src[]
Clamp Hold a value at a limit instead of going past it 99999999999 → INT_MAX
INT_MAX / INT_MIN The largest / smallest int, in <limits.h> 2147483647 / −2147483648
long long An integer type of at least 64 bits up to about 9.2 × 10¹⁸
Signed overflow A signed result too big for its type INT_MAX + 1
Undefined behaviour (UB) The C standard makes no promise about the result signed overflow
c - '0' A digit char's value '7' - '0' = 7
isdigit / isspace True for '0'..'9' / whitespace isdigit('+') is false
errno, ERANGE A global error code; ERANGE means out of range set by strtol
snprintf printf into an array, at most its size, always '\0' snprintf(d, sizeof d, "%s", s)
-Warray-compare gcc warning (in -Wall) for comparing two arrays a == b

Summary

  • Arrays can't be assigned: copy char by char, and copy the '\0'; dst must have room. There is no + for strings: strcat walks to the '\0' and copies there, and in a loop you keep the length (else O(n²)).
  • a == b compares addresses; strcmp compares text, the first difference decides, and the end of a string counts. Only the sign is guaranteed.
  • String to integer: skip spaces, one sign, digits until a non-digit; build in a long long and clamp to INT_MAX/INT_MIN, because signed overflow is undefined behaviour. Real code: strtol.

Next episode: Matrix questions: add and multiply matrices (when multiplication is possible), transpose, spiral order print.