Shuffle text

Asked

Viewed 963 times

-1

insira o código aqui[![inserir a descrição da imagem aqui][1]][1]

shuffle text

Rules: 1. Punctuation marks are word separators, are not part of words and so are not shuffled, for example:

a) "bacteria." was scrambled as "bitcaaarés.". b) "(agency" got scrambled as "(aignca". Note that the end point has not changed position. Consider at least the following special characters such as punctuation/separation marks: • ' r' • ' n' • ' ' • ',' • '. ' • '? ' • '" • ' ' • '! ' • '" • '" • ':' • '(' • ')' • '[' • ']' • '{' • '}' • '-' • '-' 2. Words with 2 or 3 letters are not shuffled: a) "Vox" continues as "Vox". b) "os" remains "os". 3. When a word is shuffled, the first and last letter may not change position. a) "calm" was shuffled as "cmala". b) "fungus" was scrambled as "fonugs".

  • and what is your doubt?

  • Everything kk, I am beginner in this language

  • @Feliperodrigues First step: click [Edit] and format your question. If you need a formatting guide, see https://answall.com/editing-help

1 answer

0

It remains to check if the word has less than 3 letters, but it seems to be here the most important:

static void Main(string[] args)
{
    var rnd = new Random();

    char[] especiais = { '\r', '.', '?' };

    string palavra = "bactérias.";

    char? especial = null;

    // caso a palavra termine com um caractere especial
    if (especiais.Contains(palavra.Last()))
    {
        // guarda o caractere especial
        especial = palavra.Last();

        // remove o caretere especial
        palavra = palavra.Substring(0, palavra.Length - 1);
    }

    // Skip(1)                   : salta o primeiro elemento
    // Take(palavra.Length - 2)  : trás os elementos consuante o tamanho da palavra menos 2
    // OrderBy(c => rnd.Next())  : faz uma ordenação aleatória dos elementos
    // ToArray()                 : converte para array
    char[] chars_do_meio = palavra.Skip(1).Take(palavra.Length - 2).OrderBy(c => rnd.Next()).ToArray();

    // new string(chars_do_meio): cria uma string a partir de um array de chars
    string resultado = $"{palavra.First()}{new string(chars_do_meio)}{palavra.Last()}{especial}";

    Console.WriteLine(resultado);

    Console.ReadKey();
}

see working in dotnetfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.