So, I'm trying to write a function that takes a string and changes all lowercase values to uppercase. Here's the code:
void lowerToUpper(char *s)
{
char *p;
for (p = s; *p; p++)
{
if (islower(*p))
*p = toupper(*p);
}
}
int main (int argc, char * argv[])
{
char *pa;
pa = "This is a test.";
printf("The following string will be edited:\n");
printf("%s\n%s\n%s\n", pa);
lowerToUpper(pa);
printf("The string has been edited, and is now as follows:\n");
printf("%s\n%s\n%s", pa);
return EXIT_SUCCESS;
}
The problem arises from the line "*p = toupper(*p);", which is where I get a segmentation fault. My guess is that the problem arises from trying to assign the value that toupper(*p) returns to *p. After doing some tests, it seems as though toupper(*p) works, but as soon as I try to assign the value to *p, I seg fault? Any ideas as to why this would happen?