This logic may change from language to language (e.g. English may have different rules than German).
Most frontend frameworks support such features and you can turn them on. E.g. in C#, you have the TextBlock.IsHyphenationEnabled for this.
If you have to write this, that sounds like a homework to me. For such things, you should make your best effort to implement this and share your code for review or if you get stuck.
The idea is that you find where a new syllable starts (typically a consonant followed by a vowel) and you put the hyphen before the new syllable.