1,538 views
1 votes
1 votes
#include <stdio.h>
void my_toUpper(char* str, int index)

{    

     *(str + index) &= ~32;

}
int main()

{

    char* arr = "gatesquiz";

    my_toUpper(arr, 0);

    my_toUpper(arr, 5);

    printf("%s", arr);

    return 0;

}

(a) GatesQuiz      

(b) gatesquiz

(c) Compiler dependent

2 Answers

4 votes
4 votes
Firstly it needs to be:

char arr[] = "gatesquiz"

char *arr = "gatesquiz" would result in segfault because in my_toUpper(), arr gets modified.

Coming to the question, the answer would be option A - "GatesQuiz".

Explanation:

Ascii value of 'a': 0110 0001

Ascii value of 'A': 0100 0001

Note that the only bit that is different is 5th bit. 'a' has 5th bit set, while 'A' has it off.

So to convert lower-case to upper case, we need to turn off only the 5th bit, ie, AND with ~2^5, ie, ~32.
1 votes
1 votes
it is compiler dependent

because &= ~32 is similar to subtracting the bit from 32, but a good way of subtraction

using biwise AND with ~32 is a bad programming practice, it used to replace the bit present with the uppercase of it and is depenedent on the bit representaion used

therefore varies according to the compiler

Related questions

1 votes
1 votes
1 answer
1
Abhisek Saha asked Nov 15, 2017
695 views
What will be the output ?#include<stdio.h>int main(){ char *str1 = "xyz"; char *str2 = "xyz"; if(str1 == str2) printf("equal"); else printf("une...
1 votes
1 votes
1 answer
2
Akash Kumar Roy asked Apr 21, 2018
373 views
"If a string constant is given then through by a pointer we can access the characters but we can't change any of the characters"Can someone please expain this?
1 votes
1 votes
1 answer
3
Harikesh Kumar asked Jan 14, 2018
353 views
step by step compile it and provide output