Day 7 | Word | C++
- Ayush Mahajan
- Aug 28, 2022
- 1 min read
We are at one week mark on consistency. Today we are solving the question 'Word': https://codeforces.com/contest/59/problem/A… Give it a try yourself.
Honestly, as a competitive programmer, it will be a bad decision to not use library functions present in cctype, but that is true in contests when speed matters a lot. In practice, we should re-implement stuff as it is the best way to revise old concepts without specifically revising.
Let me first explain the concept of 'char'. In computers, there is nothing other than binary and char is an integer (8-bit one) with a different purpose. 8-bit can hold up to 256 values. And each value represents a different character in ASCII Table. https://rapidtables.com/code/text/ascii-table.html
If you notice, then you will see that uppercase characters, lowercase characters, and numeric characters are in a series. 'A' = 65, 'B' = 66 and so on And same is for lowercase
So If I can check like this is letter is upper case or not if(s[i] >= 'A' && s[i] <= 'Z') // then it is uppercase I can also check the same of lowercase
Further notice that 'B' - 'A' = 1 and also 'b' - ' a' = 1. Thus 'B' - 'A' = 'b' - 'a' 'B' = 'b' - 'a' + 'A' 'b' = 'B' - 'A' + 'a' Thus to change lowercase to uppercase add 'A' - 'a' And for opposite add 'a' - 'A' In other words, subtracts it's kind and add opposite kind.
You can find my solution here using char as an integer concept (there is no use of #include<cctype> here): https://codeforces.com/contest/59/submission/169593417… If you want to check how to use library functions for this: https://codeforces.com/contest/59/submission/169592424
![Day 44 | Binary Search Practice [Leetcode] | C++](https://static.wixstatic.com/media/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg)
![Day 43 | Graph and Tree Practice [Leetcode] II | C++](https://static.wixstatic.com/media/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg)
![Day 42 | Graph Practice [LeetCode] | C++](https://static.wixstatic.com/media/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg)
Comments