Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

c# - Generate a sentence from a pre defined words

I'm working on a system that generates a password for a user, place a telephonic call, and speaks the password one character at a time. For example, let the password be "BlaBla123"

Now I have this basic words:

"Capitol", "Small", and "as in"

Also, I have this table:

//  Letter | Word     | Code    |
// --------+----------+---------|
//  a      | Alpha    |  97, 65 |
//  b      | Bravo    |  98, 66 |
//  c      | Charlie  |  99, 67 |
//  d      | Delta    | 100, 68 |
//  e      | Echo     | 101, 69 |
//  f      | Foxtrot  | 102, 70 |
//  g      | Golf     | 103, 71 |
//  h      | Hotel    | 104, 72 |
//  i      | India    | 105, 73 |
//  j      | Juliet   | 106, 74 |
//  k      | Kilo     | 107, 75 |
//  l      | Lima     | 108, 76 |
//  m      | Mike     | 109, 77 |
//  n      | November | 110, 78 |
//  o      | Oscar    | 111, 79 |
//  p      | Papa     | 112, 80 |
//  q      | Quebec   | 113, 81 |
//  r      | Romeo    | 114, 82 |
//  s      | Sierra   | 115, 83 |
//  t      | Tango    | 116, 84 |
//  u      | Uniform  | 117, 85 |
//  v      | Victor   | 118, 86 |
//  w      | Whiskey  | 119, 87 |
//  x      | X-ray    | 120, 88 |
//  y      | Yankee   | 121, 89 |
//  z      | Zulu     | 122, 90 |
//  1      | One      | 49      |
//  2      | Two      | 50      |
//  3      | Three    | 51      |
//  4      | Four     | 52      |
//  5      | Five     | 53      |
//  6      | Six      | 54      |
//  7      | Seven    | 55      |
//  8      | Eight    | 56      |
//  9      | Nine     | 57      |
//  0      | Zero     | 48      |

Now, I seek to make a sentences like:

"Capitol B as in Bravo" "Small L as in Lima" "Small A as in Alpha" "Capitol B as in Bravo" "Small L as in Lima" "Small A as in Alpha" "One" "Two" "Three"

Against the supposed password.

Can anyone share a thought or so as to how I store the table in memory and just ForEach a password String and get a sentence for every encountered character?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like this:

Fill the dictionary like characterLookup.Add('a', "Alpha"), note 'ToLower' in the code example, only store the lower case letters.

A key-value store (Dictionary) is the data structure of choice because we are doing a lookup by key (in this case the letter).

    private Dictionary<char, string> characterLookup = new Dictionary<char, string>();

    public string[] GetSentences(string password)
    {
        string[] sentences = new string[password.Length];

        for (int i = 0; i < password.Length; i++)
        {
            char currentChar = password[i];

            if (char.IsLetter(currentChar))
            {
                sentences[i] = string.Format("{0} {1} as in {2}",
                    char.IsLower(currentChar) ? "Lower" : "Upper",
                    currentChar.ToUpper(),
                    characterLookup[currentChar.ToLower()]);
            }
            else if (char.IsDigit(currentChar))
            { 
                sentences[i] = characterLookup[currentChar];
            }

        }
        return sentences;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...