HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

String reversal, capitalize vowels, lowercase consonants

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lowercasevowelsconsonantscapitalizereversalstring

Problem

This was a portion of a skills test given to me by a company. The task was to write a method that takes a string, reverses it, and sets the vowels as capitals and consonants to lowercase. I was also told to prove that my implementation works.

using System;
using System.Diagnostics;

namespace StringOperation
{
    using static Console;

    internal class Program
    {
        #region Entry

        private static void Main()
        {
            while (true)
            {
                WriteLine(MessageStrings.REQUEST_NAME);
                var strResult = ReadLine();

                var result = ProcessInput(string.IsNullOrEmpty(strResult) ? MessageStrings.PROOF : strResult );

                if ((ReadKey(true).Key ^ ConsoleKey.Y) == 0)
                    continue;
                break;
            }
        }

        #endregion

        #region Logic

        private static string ProcessInput(string input)
        {
            var output = new char[input.Length];

            var desc = output.Length;
            var asnd = -1;

            while(asnd++  122)
                return target;

            switch (i)
            {

                case 97:
                case 101:
                case 105:
                case 111:
                case 117:
                    // Returning a capital vowel
                    return (char) (target & ~0x20);
                default:
                    // returning Lower case consonant
                    return (char) (i);
            }
        }

        #endregion
    }
}

Solution

We have a namespace called StringOperation,
a class called Program,
and methods called Main,
ProcessInput, Shift,
regions called Entry, Logic.

Looking from a high level,
this program could do anything,
as these terms are completely meaningless.
Strive to find more meaningful names for your program elements,
so that readers can have a clue what the program is about without having to fully read the implementation.
If I can see in front of me the logical pieces of the puzzle,
the mental burden of understanding what goes on in each element would be much reduced.

I know that all self-respecting programmers know by heart that 97 means the ASCII code of the letter A, 101 the letter E, 105 the letter I, ...
Wait a minute, no, actually that's not a requirement,
I don't think anybody should have to know at a glance what these numbers mean in your case statements.
A better way would be to write like this:

case 'A':
case 'E':
case 'I':
// ...

Code Snippets

case 'A':
case 'E':
case 'I':
// ...

Context

StackExchange Code Review Q#93140, answer score: 16

Revisions (0)

No revisions yet.