0

Why in if condition 65,82 are used?

#include <iostream> using namespace std; int main(){ char c; cin >>c; if(c>=65&&c<82) c=c+32; switch (c){ case'a': case'e': case'i': case'o': case'u': cout <<"it is vowel"<<endl; break ; default : cout<<"it is consonant"<<endl; } return 0 }

26th May 2021, 6:21 AM
Durga M
Durga M - avatar
1 Réponse
+ 3
The constants 65 and 82 are the ASCII values of characters, which is a popular encoding system for characters: https://en.m.wikipedia.org/wiki/ASCII Here, 65 is the value of the letter 'A', and 82 the value of the letter 'R'. If the user enters an uppercase letter in this range, adding 32 returns the corresponding lowercase letter, meaning you don't need separate checks for the uppercase vowels. However, I don't quite understand why 82 was chosen. The ASCII value of 'U' is 85, so for an uppercase 'U', the program would classify it as a consonant. It would make more sense to me to have the following condition: // shift range [A-U] to [a-u] if ( c >= 65 && c <= 85 ) { c += 32; }
26th May 2021, 7:08 AM
Shadow
Shadow - avatar