Skip to content

Instantly share code, notes, and snippets.

@santosh
Created September 9, 2018 04:47
Show Gist options
  • Select an option

  • Save santosh/55634850352373e4efd2ef8c9b65ea5a to your computer and use it in GitHub Desktop.

Select an option

Save santosh/55634850352373e4efd2ef8c9b65ea5a to your computer and use it in GitHub Desktop.
Get a string, make a copy of it, capitalize the copy.
#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