Before we start, think of how "gonna" and "wanna" arose in English - they are phonetic corruptions of "going to" and "want to". They are relatively common even in writing in which the writer does not intend to mimic speech. The history of can't/cannot is much the same.
New Fowler's Modern English Usage (2000) states
The reduced form can't, which now seems so natural, is relatively recent in origin. It does not occur in the works of Shakespeare, for example, and the earliest example of it given in the OED is one of 1706
In "Negation in English and other Languages" by Jespersen, he comments
In writing the forms in n't make their appearance about 1660 and are already frequent in Dryden's, Congreve's, and Farquhar's comedies. Addison in the Spectator nr. 135 speaks of mayn't, can't, sha'n't, won't, and the like as having "very much untuned our language, and clogged it with consonants". Swift also (in the Tatler nr. 230) brands as examples of "the continual corruption of our English tongue" such forms as cou'dn't, ha'n't, can't, shan't; but nevertheless he uses some of them very often in his Journal to Stella.
You will see the parallel with "gonna" and "wanna"
Cannot becomes can't with a different vowel, long [a:]; Otway 288 writes cannot, but pronounces it in one syllable [to add - can't]. Congreve 268 has can't. In the same way, with additional dropping of [l], shall not becomes [sha'nt] [to add - also with a long vowel]. The spelling was not, and is not yet, settled ; NED. records sha'nt from 1664, shan't from 1675, shann't from 1682 (besides Dryden's shan'not 1668); now both shan't and sha'n't are in use.
(Out of interest, Jespersen then goes on to refer to the lengthened "a" in the tag of "I'm angy, an't I?" in which the "m" is dropped and has given rise to the incorrect "I'm angry, aren't I?"... because that is what it sounds like...)
So, in short, cannot became can't because that is how the majority of people spoke and heard it. However, two spellings and two pronunciations are now accepted. The battle for "correctness" has ended in a compromise.
From the above, and in your example "Why cannot I do it?" I surmise that the "cannot" would have been popularly pronounced "can't" from somewhere in the early 1600s, yet spelled "cannot" and pronounced that way by teachers (who, by-and-large, are conservative.)
You will note that there are no circumstances in which can't may not replace "cannot", but "cannot" looks and sounds more "educated/formal" and may not replace "can't" in all circumstances. This is based on 400 years of pronunciation.