C strncmp
Check the beginning of a reference before routing it to the billing queue.
A simple example
strncmp
- Start with "abc" and "ab"
- Do this Compare the beginning
- Result Yes
#include <stdio.h>
#include <string.h>
int main(void) {
const char *value = "abc";
const char *other = "ab";
printf("%s\n", (strncmp(value, other, strlen(other)) == 0) ? "yes" : "no");
return 0;
}
yes
Syntax
strncmp(label, "INV-", 4) == 0
Run these examples in C
Save one example at a time as lesson.c. Use C17 and a Unix-style shell.
cc -std=c17 -Wall -Wextra lesson.c -lm -o lesson
./lessonRecognize an invoice prefix
A finance inbox recognizes invoices by a specific prefix on their reference codes. Inspect the beginning of the code before placing an incoming record in the invoice queue.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *label = "INV-482";
printf("%s\n", (strncmp(label, "INV-", 4) == 0) ? "yes" : "no");
return 0;
}
yes
The first four characters are INV-, so this order enters the invoice queue.
Route a finance request
An intake queue uses a recognizable prefix or status to route incoming work. Check the required position and comparison rules so a matching fragment in an unrelated field cannot send a request to the wrong desk.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *value = "FIN-482";
const char *other = "FIN-";
printf("%s\n", (strncmp(value, other, strlen(other)) == 0) ? "yes" : "no");
return 0;
}
yes
The prefix identifies the finance queue.
Reject a prefix in the middle
A test label contains a routing token later in its text rather than at the beginning. Confirm that the prefix check rejects it so a keyword inside an unrelated label cannot trigger routing.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *value = "OLD-FIN-482";
const char *other = "FIN-";
printf("%s\n", (strncmp(value, other, strlen(other)) == 0) ? "yes" : "no");
return 0;
}
no
The expected text occurs later, so this is a different kind of label.
What to watch for
Prefix matching here is case-sensitive. Keep the full identifier when handing it to the next step.