Skip to content

Instantly share code, notes, and snippets.

@denisdemaisbr
Created October 8, 2023 22:23
Show Gist options
  • Save denisdemaisbr/93cabdc96df22cc2f497a3f2ccd477a7 to your computer and use it in GitHub Desktop.
Save denisdemaisbr/93cabdc96df22cc2f497a3f2ccd477a7 to your computer and use it in GitHub Desktop.
a simple function to duplicate string and upper it.
/*
a simple function to duplicate string and upper it.
sample:
...
char *upper = strdup_upper("HELLO world");
if (!upper) { perror("ops!"); exit(1); }
puts(upper);
free(upper);
...
*/
char* strdup_upper(char* str) {
char *b;
char *s;
char *buf;
errno = 0;
buf = strdup(str);
if (!buf)
return NULL;
b = buf;
s = str;
while(*b) {
*b++ = toupper(*s++);
}
errno = 0;
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment