#include <stdio.h>
void my_toUpper(char* str, int index)
{
*(str + index) &= ~32;
}
int main()
{
char* arr = "geeksquiz";
my_toUpper(arr, 0);
my_toUpper(arr, 5);
printf("%s", arr);
return 0;
}
Answer: C
Correct answer: Compiler dependent
arr points to a string literal, and modifying one is undefined behaviour. Most compilers place literals in read-only memory, so the write faults at run time; others allow it, and the program prints GeeksQuiz. The fix is char arr[] = "geeksquiz";, which gives a modifiable copy. (The &= ~32 trick works because upper and lower case differ by exactly bit 5.)
Please log in or register to report this question.