25

I am trying to learn the Dart language, by transposing the exercices given by my school for C programming.

The very first exercice in our C pool is to write a function print_alphabet() that prints the alphabet in lowercase; it is forbidden to print the alphabet directly.

In POSIX C, the straightforward solution would be:

#include <unistd.h>

void    print_alphabet(void)
{
    char    c;

    c = 'a';
    while (c <= 'z')
    {
        write(STDOUT_FILENO, &c, 1);
        c++;
    }
}

int     main(void)
{
    print_alphabet();
    return (0);
}

However, as far as I know, the current version of Dart (1.1.1) does not have an easy way of dealing with characters. The farthest I came up with (for my very first version) is this:

void  print_alphabet()
{
  var c = "a".codeUnits.first;
  var i = 0;

  while (++i <= 26)
  {
    print(c.toString());
    c++;
  }
}

void main() {
  print_alphabet();
}

Which prints the ASCII value of each character, one per line, as a string ("97" ... "122"). Not really what I intended…

I am trying to search for a proper way of doing this. But the lack of a char type like the one in C is giving me a bit of a hard time, as a beginner!

Muath Amer
  • 65
  • 1
  • 8
Diti
  • 1,454
  • 3
  • 23
  • 38

2 Answers2

39

Dart does not have character types.

To convert a code point to a string, you use the String constructor String.fromCharCode:

int c = "a".codeUnitAt(0);
int end = "z".codeUnitAt(0);
while (c <= end) {
  print(String.fromCharCode(c));
  c++;
}

For simple stuff like this, I'd use "print" instead of "stdout", if you don't mind the newlines.

There is also:

int char_a = 'a'.codeUnitAt(0);
print(String.fromCharCodes(new Iterable.generate(26, (x) => char_a + x)));

or, using newer list literal syntax:

int char_a = 'a'.codeUnitAt(0);
int char_z = 'z'.codeUnitAt(0);
print(String.fromCharCodes([for (var i = char_a; i <= char_z; i++) i]));
lrn
  • 64,680
  • 7
  • 105
  • 121
  • Wow, that `Iterable` class looks powerfull! It seems overkill to me, but I guess you can do things with it that would take longer to write in a loop statement. – Diti Jan 17 '14 at 11:42
  • @Irn that's a very strong and fast. Your example for Latin1. My question is how can I do this in ISO 8859-9 ("Latin 5")? – Nick Mar 29 '19 at 14:31
  • For a non-Latin-1 code page, you need to do your own conversion between bytes and Unicode characters. Since it only has [six differences from Latin-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-9), the easiest is probably to just check for those: `cpToLatin9(int cp) => const {0x11e: 0xd0, 0x11f: 0xf0, 0x130: 0xdd, 0x131: 0xfd, 0x15e: 0xde, 0x15f: 0xfe}[cp] ?? cp` and `latin9ToCp(int latin9Char) => const {0xd0: 0x11e, 0xf0: 0x11f, 0xdd: 0x130, 0xfd: 0x131, 0xde: 0x15e, 0xfe: 0x15f}[latin9Char] ?? latin9Char`. When you get a iso08859-9 byte, you can use `latin9toCp` to get a code point for it. – lrn Apr 01 '19 at 05:43
  • @Irn I try to implement and didn't catch exactly. Example In my Dart code İ capital I with dot above represent as byte[304] and I have to replace this with server byte[152] and send to server as byte not a string. How does cpToLatin9(int cp) and latin9ToCp(int latin9Char) works? – Nick Apr 02 '19 at 14:28
  • The functions just convert code points (`cp`) to bytes in the codepage for Latin-9, and from bytes to code points again. The former is not safe against invalid values, so use with care (and I misnamed the functions, it should be latin5, not latin9, because confusingly the former is ISO-8859-9 and the latter is ISO-8859-15). In any way you can use `cpToLatin5(304)` to get the value 152. That's a byte, and you can just use it as a byte where needed. – lrn Apr 05 '19 at 12:59
2

As I was finalizing my post and rephrasing my question’s title, I am no longer barking up the wrong tree thanks to this question about stdout.

It seems that one proper way of writing characters is to use stdout.writeCharCode from the dart:io library.

import 'dart:io';

void  ft_print_alphabet()
{
  var c = "a".codeUnits.first;

  while (c <= "z".codeUnits.first)
    stdout.writeCharCode(c++);
}

void main() {
  ft_print_alphabet();
}

I still have no clue about how to manipulate character types, but at least I can print them.

Community
  • 1
  • 1
Diti
  • 1,454
  • 3
  • 23
  • 38
  • 4
    Question: "I still have no clue about how to manipulate character types", Answer: "Dart does not have character types. They are just an integers. So you can manipulate them as with integers because they are integers". – mezoni Jan 17 '14 at 12:49