Tuesday, September 29, 2015

String Functions

Strings are sequences of characters. They are useful for storing data and also for accepting user input, whether it be from a terminal command line or a web form.

I find these particular string functions indispensable in my own web programming. I'm sure there are many other frequently-used string functions.

ActionC++PHPJavaScript
Split a string into an array of strings–explodesplit
Compare stringscomparestrcmp< or >
Find a character within a stringfindstrchrindexOf
Get length of stringsizestrlenlength
Find a string within a stringstrstrstrstrindexOf
Convert a string to hexadecimalstd::hex–toString(16)
Get a substringsubstrsubstrsubstring
Convert string to floating pointatoffloatvalparseFloat
Convert string to integeratoiintvalparseInt
Upper case first charactertoupperucfirsttoUpperCase
Trim white space–trimtrim
Replacereplacestr_replacereplace
Regular expression replacecpreg_replacereplace
Determine if string is a number–is_numeric!isNan

There are also a few useful functions that relate to single characters only, but they are usually used in the context of string processing.

ActionC++PHPJavaScript
Get the ASCII value of a character–chrfromCharCode
Determine if character is a digitisdigitctype_digitparseInt and slice
Determine if a character is upper caseisupperctype_uppercompare with toUpperCase


Sunday, September 27, 2015

ASCII Character Set

Hex Decimal Octal



Decisions Using the Switch Statement

You can also make decisions using the switch statment. It looks like this:

switch (exponent) {
    case 0 :
        // do nothing
        break;
    case 1 :
        input = input * 10;
        break;
    case 2 :
        input = input * 100;
        break;
    case 3 :
        input = input * 1000;
        break;
    default :
        // do nothing
        break;
}

The above code implements simple exponentiation for languages that do not support it natively. Note that each case is an integer. The general rule is that the different cases must be types that can be converted to integer at compile time. The computer science term for such types is "enumerable." Integers, characters, and enumerations would be valid here. (An enumeration is just a way of giving names to different integers, like constant literals.) Characters are valid enumerable types because they can be considered a restricted set of integers. For example, the letter 'A' evaluates to the integer 65. See the ASCII character set for more information.

Friday, September 25, 2015

Programmer's Code Editor

It is always a good idea to use a good programmer's text editor for any kind of code development. Important features of a programmer's text editor include:
  • Syntax code coloring
  • Language-aware auto indent and formatting
  • Strict text file save with line ending options
If you are doing PHP or JavaScript programming and you don't need an IDE but you do need an editor, I recommend NotePad++ for Windows, TextMate for Mac, and Brackets for either Windows or Mac. I no longer recommend TextWrangler because it does not have language-aware formatting.

Please, never use a program like NotePad, WordPad, Microsoft Word, or TextEdit to edit code. They do not have the proper options to safely save your files as strict text. They also do not have code coloring or auto indent.


Integrated Development Environment (IDE)

If you are learning C++, Java, Swift, iOS programming, or Android programming, it is helpful, and maybe necessary, to use an integrated development environment (IDE) to write, debug, and test your programs.

An IDE contains all the traditional programming needs in one package:
  • Code editor
  • Debugger
  • Simulator
  • Project management
  • User interface development

Thursday, September 24, 2015

HTML Entities

These are some HTML entities I have used on this blog or use from time to time. This is not an exhaustive reference. These are just the ones I found most useful.

Beware: not all browsers support all these entities, especially the UTF-8 shapes. I have found this particularly true on mobile browsers.

EntityAppearanceName


Decisions and Branching

We must have a way for the program to make a decision, and then execute different code based on the decision. If the number is less than 10, we do one thing, otherwise we do something else. If the object is a rectangle, we obtain the area by multiplying its width by its height; but if the object is a circle, we obtain its area by multiplying its diameter by π.

Indeed, I would venture a guess that the majority of the time, popular computer programs are making decisions. (This is less so for scientific programs that do extensive numerical computations.)

The Resistor Color Code

I use this color code as an example in my classes for writing branching clauses, loops, and functions.

0Black
1Brown
2Red
3Orange
4Yellow
5Green
6Blue
7Violet
8Gray
9White

The color code works like this: there are (at least) three color bands on the resistor. The first band indicates the first digit in the resistance. The second band indicates the second digit. The third digit is a multiplier. 

So, for example, if the bands were Yellow, Violet, and Orange, this translates to 4, 7, and 3. The  resistance value would be 47 × 1000 or 47 K Ω.

The computation would look like this:

R = ((10 * a) + b) * 10 c

where a, b, and c are the translated values of the three color bands, in order.


Foundational Programming Concepts

I teach three programming classes. There are several concepts that I must teach in all my classes because they are so foundational. Once you have mastered these concepts in any programming language, it is easy to transfer the knowledge to other languages.

Wednesday, September 23, 2015

Typing the Apple Symbol

After seeing my post how to type the command key / cloverleaf symbol, someone asked for a similar post how to draw the Apple symbol.

As a digression, this is a story I heard. In the early days of Apple, when Susan Kare was designing icons for the first Macintosh operating system, they used the Apple symbol on the keyboard command key, so she put the Apple symbol in all the menus for keyboard shortcut. When Steve Jobs saw this, he something to the effect of "Apples everywhere, too many apples!"

Look now in the File or Edit menu of your Mac, and see all the keyboard shortcuts there. Imagine if these were all Apple symbols! Now you can imagine why SJ was taken aback.

So Susan Kare looked for a distinctive but non-threatening symbol, and she found the cloverleaf symbol in a book of symbols used on Swedish maps to indicate a point of interest while traveling.

Anyway, beware how you use the Apple symbol. As a solid symbol, instead of an outline, it can be imposing. In an HTML document (web page), you can type the Apple symbol by adding this HTML entity to your code:

&#63743;

It should look like this:


If you need to type the Apple symbol into a word processing document or other document on your Mac, the keyboard shortcut is Option-Shift-K.