Skip to content

Instantly share code, notes, and snippets.

@jacobabrahamb4
Last active December 16, 2015 03:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jacobabrahamb4/5370939 to your computer and use it in GitHub Desktop.
convert to upper case
// find more explanations here
// https://www.securecoding.cert.org/confluence/display/seccode/STR30-C.+Do+not+attempt+to+modify+string+literals
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *strptr = NULL, *copy = NULL;
strptr = "Jacob Abraham"; // this will be stored in a read only location hence cannot be modified.
char str[]="Jacob Abraham"; // instead if str is used , it can be used to modify the array at anytime during the runtime.
int i, size=0;
printf("%p \n",strptr);
for(copy=strptr;*copy!='\0';copy++,size++,printf("original --> %c \n",*copy))
{
printf("%c \n",(*copy>='a'&& *copy<='z')?('A'+*copy-'a'):(*copy));
//*copy=(*copy>='a'&& *copy<='z')?('A'+*copy-'a'):(*copy); // this is not allowed.
}
printf("%p \n",strptr);
printf("%s", strptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment