Does anyone have any books, articles, sources, where it is clearly and clearly written about it? I myself am a student, so do not judge strictly) We are taught C ++, but for some reason it occurred to me to write it in Java.

  • four
    What and what you want to broadcast? - avp
  • 3
    and what's the problem? think up an algorithm, write the code =) - Gorets
  • 2
    What's the problem? make a symbol translation table (you have one on one, right?), and convert all characters using it. - VladD
  • 2
    > Enter the text, then in advance into the alphabet, it is translated into another. that is, just a substitution? so this is a three-line program - Nofate ♦
  • five
    so here the problem is not with the language, but with your own thinking. Called Laziness - Viacheslav

3 answers 3

The task is not very clear, so it is difficult to immediately advise. First you need to know the timing, as well as the task itself.

Enter the text, then in advance into the alphabet, it is translated into another.

Is there a one-to-one correspondence between each character of one grammar and another? Those. Does each character from the input grammar always correspond to one character from another? If yes, then the implementation is quite simple and trivial, part of the algorithm you already described, for example, KryDos.

Or, when translating the current character, the output character depends on the previous one? If the situation is as follows, then you can build a finite state machine (Moore’s machine for example), it’s not so difficult, there’s a lot of material on this on the Internet, so I’m not going to paint in detail, especially since your situation may be easier. In simple terms, with each character applied to the input, you will go into a certain state of the machine, which “expects” that certain other characters will be input (if the “unexpected” character is supplied, then the autamut goes into an error state and, roughly speaking, work can and finish). For example, only characters from the set {a, b, c} can be input, and characters from the {d, e, f} set can be output. Suppose you need the first character to be 'a' or 'b'. If the character 'a' was entered, you expect to enter 'c' and output 'd', if you enter the character 'b' - you expect to enter 'b' (again) and then output the output f '. A transition table is compiled: first, we are in some initial state (initial for example), then, if the 'a' symbol is input, then we go to the state a1, if the character is 'b', then we go to the state b1 and t .d

For all these transitions, in essence, you need to build this same transition table. For example, in my notebook and \ or MS Excel \ LibreOffice Calc, etc.

I suspect that most likely you need to implement some 1 of the above two cases. In general, ideally, if you want to deal with this topic more seriously, then you can just read books, for example the same "dragon book", which you have already been advised in the comments (there is enough literature on this topic). But most of these simple tasks like yours can be solved either by simple trivial algorithms, finite automata, or recursive descent. In more complex cases (when you need to make LL (k) grammar, etc.) - you need to write automata with stack memory and \ or use generators like Ragel, Lemon.

    Well, in general it would be right to lay out their attempts to solve. And so it turns out that we are doing everything for you.

    Solution head-on (algorithm).

    Create two arrays. In one Russian alphabet, in the other - your own. The string to be translated into another alphabet is broken down into an array of characters. In the cycle we look at each symbol and position in which it is in the Russian alphabet. We replace it with a symbol from your alphabet, which is in the same position as the original symbol in the Russian alphabet. Or you can do with one associative array (HashMap which is in java).

    I hope, clearly described.

      What do you understand as a translator? A dictionary program? A simple version of two arrays of strings, more difficult - a collection of objects with a field pointing to the language, with a field for the original meaning, with a collection of references to synonyms, with a collection of references to literal translations, and then just sampling from this heap. If you make an interpreter of the language, then the compiler / interpreter theory is freely available, there is a rather thin book on the toad, where at the end a small interpreter of the Vasik, the name of the book forgot