top of page
Search

Day 7 | Word | C++

  • Writer: Ayush Mahajan
    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


 
 
 

Comments


bottom of page