Theory
Match or not?
strcmp, from string.h, compares two strings:
char name[10] = "Riya";
if (strcmp(name, "Riya")) printf("Match");
else printf("No match");
What prints?
Think first
Your guess first
Type what it prints.
Show the answer
No match. strcmp measures difference: equal strings give 0, and 0 is false. Test equality with if (strcmp(a, b) == 0); plain if (strcmp(a, b)) is true when they differ.
Theory
The fence
These string.h functions are ready-made loops walking to a string's fence, its null terminator '\0': strlen counts the characters before it.
Practical
strlen or sizeof?
#include <stdio.h>
#include <string.h>
int main() {
char city[20] = "Surat";
int len = strlen(city);
int size = sizeof(city);
printf("%d %d", len, size);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
What does the program above print?
- 5 20
- 6 20
- 5 6
- 5 5
Show the answer
5 20
strlen stops at the fence without counting it: 5. sizeof gives the array's capacity: 20 bytes, whatever is stored. 6 20 counts the fence.
Think first
Spot the error
char name[10];
name = "Riya";
Why will line 2 not compile? Fix it.
Show the answer
An array name is a constant address, a signpost at box zero, and signposts do not move. Copy instead: strcpy(name, "Riya");, destination first, like =. Initializing at declaration is fine.
Think first
Room enough?
strcat(dest, src) appends src at dest's '\0': your Unit 3 merge, ready-made.
char full[8] = "Riya";
strcat(full, " Shah");
What goes wrong?
Show the answer
No room: Riya Shah plus '\0' needs 10 boxes; full has 8. It compiles, but strcat writes past the end (C never resizes arrays), corrupting memory.
Think first
Standard C?
strrev(s) reverses s: "Riya" becomes "ayiR". Is it standard C?
Show the answer
No. Turbo C has it, like conio.h, but it is non-standard. On Linux gcc, write the reverse loop yourself.
Theory
Which comes first?
strcmp(a, b) compares character by character, by character code, to the first difference: negative if a comes first, positive if b does. Only the sign is promised.
Practical
Exam-style: predict the output
#include <stdio.h>
#include <string.h>
int main() {
char a[20] = "Riya";
char b[20] = "Rohan";
char first[20];
int n;
strcat(a, " Shah");
if (strcmp(a, b) < 0)
strcpy(first, a);
else
strcpy(first, b);
n = strlen(first);
printf("%s %d", first, n);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
Exam-style: what does the program above print?
- Riya Shah 9
- Riya Shah 10
- Rohan 5
- Riya Shah 8
Show the answer
Riya Shah 9
a becomes Riya Shah. After the R's, 'i' comes before 'o', so strcmp is negative and first gets a: 9 characters, space included. Rohan 5 compares lengths or flips the sign; Riya Shah 10 counts the '\0'.
Summary
Key takeaways
- strcmp gives 0 when EQUAL, negative or positive for order; test == 0.
- strlen stops before the '\0'; sizeof gives the capacity.
- Arrays refuse =: strcpy copies, strcat appends, destination first, with room; strrev is non-standard.
- Memory hook: walk to the fence; zero difference means equal.