Thread overview
How to code Z-Function of string?
Mar 26, 2020
Quantium
Mar 26, 2020
Dennis
Mar 27, 2020
WebFreak001
Mar 26, 2020
MoonlightSentinel
March 26, 2020
I cannot make a code that is calculating Z-Function of string. Main trouble is that string is immutable char[].
1. How can I make string ONLY char[] (Not immutable)
2. How can I work with some of chars in the stirng, is, for example:
    string s="abc";
    writeln(s[1]); // Should write 'b' or not???
Is it a legit code or it doesn't work ?
March 26, 2020
On Thursday, 26 March 2020 at 19:34:08 UTC, Quantium wrote:
> 1. How can I make string ONLY char[] (Not immutable)

You can use .dup to make a mutable copy of an array.

```
char[] a = "abc".dup;
```

> 2. How can I work with some of chars in the stirng, is, for example:
>     string s="abc";
>     writeln(s[1]); // Should write 'b' or not???
> Is it a legit code or it doesn't work ?

Yes, that works.
March 26, 2020
On Thursday, 26 March 2020 at 19:34:08 UTC, Quantium wrote:
>     string s="abc";
>     writeln(s[1]); // Should write 'b' or not???
> Is it a legit code or it doesn't work ?

Yes, but keep in mind that char[] implies UTF8. Hence this won't work if your input contains e.g. chinese characters.

Refer to std.utf (https://dlang.org/phobos/std_utf.html) for some utilities to deal with UTF.
March 27, 2020
On Thursday, 26 March 2020 at 20:08:39 UTC, Dennis wrote:
> On Thursday, 26 March 2020 at 19:34:08 UTC, Quantium wrote:
>> 1. How can I make string ONLY char[] (Not immutable)
>
> You can use .dup to make a mutable copy of an array.
>
> ```
> char[] a = "abc".dup;
> ```

extending on this: if you want to work on single unicode codepoints instead of utf 8 encoded bytes, you can use:

char[] a = "abc".to!(char[]); // utf8 encoded bytes - single bytes may at most cover basic ASCII characters
wchar[] a = "abc".to!(wchar[]); // utf16 encoded bytes - each element is ushort size, covers all characters of the Basic Multilingual Plane, and otherwise uses surrogate pairs which you might want to check for. Emoji span 2 code units for example
dchar[] a = "abc".to!(dchar[]); // utf32 - each element is uint, covers all possible code points

however note that code points may not cover all characters in all languages. Characters might be composed of multiple code points, even relatively innocent looking characters like รค may be composited of an umlaut code point and the character 'a'

Basically before doing any operations on your text, I would specify and check for rules to restrict what your input can be. For example if you only want characters from certain ranges such as latin characters, restrict the user from entering anything else. std.uni can help you greatly with working with these ranges and std.utf can help you decode utf8/utf16/utf32 if you choose not to convert to char[], wchar[] or dchar[] before.