trim_leading_blanks

This commit is contained in:
Simon Forman 2023-02-01 16:40:14 -08:00
parent b8b37af1a4
commit 2f71db945a
1 changed files with 16 additions and 3 deletions

View File

@ -2,17 +2,30 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
const char *text = "hi there friend"; const char *BLANKS = " \t";
const char *TEXT = " hi there friend";
/* 01234567890123456789 /* 01234567890123456789
^ ^ ^ ^
*/ */
char *
trim_leading_blanks(char *str)
{
size_t offset = strspn(str, BLANKS);
if (offset == strlen(str)) {
/* All blanks. */
return NULL;
}
return str + offset;
}
int int
main(void) main(void)
{ {
char *rest; char *rest, *text;
ptrdiff_t diff; ptrdiff_t diff;
text = trim_leading_blanks((char *)TEXT);
rest = strpbrk(text, " []"); rest = strpbrk(text, " []");
while (NULL != rest) { while (NULL != rest) {
diff = rest - text; diff = rest - text;