Created
September 9, 2018 04:47
-
-
Save santosh/55634850352373e4efd2ef8c9b65ea5a to your computer and use it in GitHub Desktop.
Get a string, make a copy of it, capitalize the copy.
This file contains hidden or 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> | |
| #include <string.h> | |
| #include <stdlib.h> | |
| #include <ctype.h> | |
| int main(void) { | |
| // define a string (or get from user input) | |
| char *s = "santosh"; | |
| // copy that string into memory | |
| // otherwise it will just reference to it | |
| char *t = malloc((strlen(s) + 1) * sizeof(char)); | |
| // the real copying goes here | |
| for (int i = 0, n = strlen(s); i <= n; i++) { | |
| t[i] = s[i]; | |
| } | |
| // make the first char capital | |
| if (strlen(t) > 0) { | |
| t[0] = toupper(t[0]); | |
| } | |
| printf("s: %s\n", s); | |
| printf("t: %s\n", t); | |
| // to prevent memory leak | |
| free(t); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment