Monday, January 30, 2017

D - Characters

Characters are the building blocks of strings. Any symbol of a writing system is called a character: letters of alphabets, numerals, punctuation marks, the space character, etc. Confusingly, the building blocks of characters themselves are called characters as well.

the integer value of the lowercase a is 97 and the integer value of the numeral 1 is 49. These values have been assigned merely by conventions when the ASCII table has been designed.
Following table gives you details about standard character types with storage sizes and its purpose.
characters are represented by the char type, which can hold only 256 distinct values. If you are familiar with the char type from other languages, you may already know that it is not large enough to support the symbols of many writing systems.
TypeStorage sizePurpose
char1 byteUTF-8 code unit
wchar2 bytesUTF-16 code unit
dchar4 bytesUTF-32 code unit and Unicode code point
Some useful character functions are listed below
  • isLower: is a lowercase character?
  • isUpper: is an uppercase character?
  • isAlpha: is a Unicode alphanumeric character (generally, a letter or a numeral)?
  • isWhite: is a whitespace character?
  • toLower: produces the lowercase of the given character
  • toUpper: produces the uppercase of the given character
import std.stdio;
import std.uni;

void main()
{
    writeln("Is ğ lowercase? ", isLower('ğ'));
    writeln("Is Ş lowercase? ", isLower('Ş'));

    writeln("Is İ uppercase? ", isUpper('İ'));
    writeln("Is ç uppercase? ", isUpper('ç'));

    writeln("Is z alphanumeric? ",       isAlpha('z'));

    writeln("Is new-line whitespace? ",  isWhite('\n'));
    writeln("Is underline whitespace? ", isWhite('_'));

    writeln("The lowercase of Ğ: ", toLower('Ğ'));
    writeln("The lowercase of İ: ", toLower('İ'));

    writeln("The uppercase of ş: ", toUpper('ş'));
    writeln("The uppercase of ı: ", toUpper('ı'));
}
When we run the above program, we will get the following output
Is ğ lowercase? true
Is Ş lowercase? false
Is İ uppercase? true
Is ç uppercase? false
Is z alphanumeric? true
Is new-line whitespace? true
Is underline whitespace? false
The lowercase of Ğ: ğ
The lowercase of İ: i
The uppercase of ş: Ş
The uppercase of ı: I

Reading Characters

We can read characters using readf as shown below.
readf(" %s", &letter);
Since D programming support unicode, in order to read unicode characters, we need to read twice and write twice to get the expected result. This don't work on the online compiler. The example is shown below.
import std.stdio;

void main()
{
    char firstCode;
    char secondCode;

    write("Please enter a letter: ");
    readf(" %s", &firstCode);
    readf(" %s", &secondCode);

    writeln("The letter that has been read: ",
            firstCode, secondCode);
}
When we run the above program, we will get the following output
Please enter a letter: ğ
The letter that has been read: ğ

No comments:

Post a Comment