Tuesday, September 21, 2010

TYPECAST: more than just a band



"Unlonely nights, romantic moments" *kidding*

Just a quick note directed to a particular firstyear who says he is engaging problems concerning the CAPITALIZATION OF LETTERS (and the reverse of it).

But before that let me just say that We all feel pleased about your eagerness to do programming stuff. We commend you for that and do continue keeping on. Cheers!

Ok, moving on, there is such a thing in C++ programming called TYPECASTING (a roughly similar JAVA tool is PARSING) that allows you to force a declared variable into a data type you desire at Run time. To address the question on capitalizing your letters, think of it as
  • forcing the input lowercase letter to be a number
  • to which you will add a value
  • where that value, when converted back to a letter
  • will output its corresponding Uppercase equivalent.
Before you continue reading the article, it would aid that you search or google up STANDARD ASCII CHARACTER SET in the interweb so as to understand what is written above in bullets,

IMPORTANT: the ascii set is the link between your lowercase and uppercase letters.

take this code fragment for instance,

char sampleLetter = 'j';
int sampleLetterBecomesInt = (int)sampleLetter; //typecasting happens here
sampleLetterBecomesInt = sampleLetterBecomesInt - 32;
cout << (char)sampleLetterBecomesInt; //and here
______________________________________

  • Why do we need to subtract by 32???
  • after line 2, what now is the value of variable sampleLetterBecomesInt
  • after line 4, what now is the output in the console (or terminal)
  • ___________________________________


    Once your done, you're ready for exercises. If you've followed the article on strings (older posts) then you are already equipped with necessary logic to do the following.
    • given a string variable with the value of "your_name"(i mean really your name as in Jessa or Martin) written in lowercase, Traverse the string capitalizing the first letter of your name.
    [martin becomes Martin and jessa becomes Jessa]
    • then you may try the reverse
    [martin becomes martiN and jessa becomes jessA]
    • then you may try CamelCaps in compound words
    [watermelon becomes WaterMelon and cheezepaper becomes CheezePaper]
    • or do it in regular intervals
    [wavylookingtext becomes WaVyLoOkInGtExT]
    // odds become capital evens are retained

    -o0o-

    0 <- post your comments here: