Last active
February 15, 2020 16:23
-
-
Save mrcat323/a2362fe7328ec0b65c7a3062c7e0af73 to your computer and use it in GitHub Desktop.
Writing ctype.h std library - additional function added: capitalize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int isdigit(int); | |
int islower(int); | |
int isupper(int); | |
int tolower(int); | |
int toupper(int); | |
int isspace(int); | |
int isprint(int); | |
void capitalize(char s[]); | |
int main(void) | |
{ | |
char s[] = "Lorem ipsum mr. Cat"; | |
int i; | |
for (i = 0; s[i] != '\0'; ++i) | |
if (!isspace(s[i]) && islower(s[i])) | |
s[i] = toupper(s[i]); | |
printf("Upped string: %s\n", s); | |
for (i = 0; s[i] != '\0'; ++i) | |
if (!isspace(s[i]) && isupper(s[i])) | |
s[i] = tolower(s[i]); | |
printf("Lowed string: %s\n", s); | |
capitalize(s); | |
printf("New string: %s", s); | |
return 0; | |
} | |
int isdigit(int c) | |
{ | |
return c >= '0' && c <= '9'; | |
} | |
int islower(int c) | |
{ | |
return c >= 'a' && c <= 'z'; | |
} | |
int isupper(int c) | |
{ | |
return c >= 'A' && c <= 'Z'; | |
} | |
int tolower(int c) | |
{ | |
return c + 'a' - 'A'; | |
} | |
int toupper(int c) | |
{ | |
return c - 'a' + 'A'; | |
} | |
int isspace(int c) | |
{ | |
return c == ' '; | |
} | |
int isprint(int c) | |
{ | |
return c >= 32 && c <= 126; | |
} | |
void capitalize(char s[]) | |
{ | |
int i; | |
i = 0; | |
while (s[i] != '\0') { | |
if (s[i] != ' ' && s[i - 1] && s[i - 1] == ' ') { | |
s[i] = toupper(s[i]); | |
} else if (s[i] != ' ' && !s[i - 1]) | |
s[i] = toupper(s[i]); | |
++i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment