Inside Out

An example should clarify: 'taxi' would become 'atix' 'taxis' would become 'atxsi'
 avatar
unknown
csharp
4 years ago
767 B
5
Indexable
string InsideOut(string x) {
    int halfStringNum = x.Length/2;
    Stack<char> charStack = new Stack<Char>();
    string output = "";
    
    for (int i = 0; i < halfStringNum; i++)
    {
        charStack.Push(x[i]);
    }
    while(charStack.Count > 0)
        output += charStack.Pop();
    
    if ((x.Length - (halfStringNum * 2)) == 1)
    {
        output += x[halfStringNum];   
        for (int i = halfStringNum + 1; i < x.Length; i++)
        {
            charStack.Push(x[i]);
        }
    }
    else
    {
        for (int i = halfStringNum; i < x.Length; i++)
        {
            charStack.Push(x[i]);
        }
    }
    while(charStack.Count > 0)
        output += charStack.Pop();
        
    return output;
}
Editor is loading...