- D - Conditional Compilation
- D Programming - Contract
- D Programming - Exception Handling
- D Programming - Concurrency
- D Programming - File I/O
- D Programming - Immutables
- D Programming - Templates
- D Programming - Modules
- D Programming - Mixins
- D Programming - Aliases
- D Programming - Ranges
- D Programming - Unions
- D Programming - Structs
- D Programming - Tuples
- D Programming - Pointers
- D Programming - Associative Arrays
- D Programming - Arrays
- D Programming - Strings
- D Programming - Characters
- D Programming - Functions
- D Programming - Decisions
- D Programming - Loops
- D Programming - Operators
- D Programming - Literals
- D Programming - Enums
- D Programming - Data Types
- D Programming - Variables
- D Programming - Basic Syntax
- D Programming - Environment
- D Programming - Overview
- D Programming - Home
D Programming - Object Oriented
- D Programming - Abstract Classes
- D Programming - Interfaces
- D Programming - Encapsulation
- D Programming - Overloading
- D Programming - Inheritance
- D Programming - Classes & Objects
D Programming - Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
D Programming - 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.
The following table mentions standard character types with their storage sizes and purposes.
The characters are represented by the char type, which can hold only 256 distinct values. If you are famipar 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.
Type | Storage size | Purpose |
---|---|---|
char | 1 byte | UTF-8 code unit |
wchar | 2 bytes | UTF-16 code unit |
dchar | 4 bytes | UTF-32 code unit and Unicode code point |
Some useful character functions are psted below −
isLower − Determines if a lowercase character?
isUpper − Determines if an uppercase character?
isAlpha − Determines if a Unicode alphanumeric character (generally, a letter or a numeral)?
isWhite − Determines if a whitespace character?
toLower − It produces the lowercase of the given character.
toUpper − It 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-pne whitespace? ", isWhite( )); writeln("Is underpne whitespace? ", isWhite( _ )); writeln("The lowercase of Ğ: ", toLower( Ğ )); writeln("The lowercase of İ: ", toLower( İ )); writeln("The uppercase of ş: ", toUpper( ş )); writeln("The uppercase of ı: ", toUpper( ı )); }
When the above code is compiled and executed, it produces the following result −
Is ğ lowercase? true Is Ş lowercase? false Is İ uppercase? true Is ç uppercase? false Is z alphanumeric? true Is new-pne whitespace? true Is underpne whitespace? false The lowercase of Ğ: ğ The lowercase of İ: i The uppercase of ş: Ş The uppercase of ı: I
Reading Characters in D
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 does not work on the onpne 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 the above code is compiled and executed, it produces the following result −
Please enter a letter: ğ The letter that has been read: ğAdvertisements