Question d'entretien d'embauche Microsoft: how to reverse words in a str... | Glassdoor.ca

Question d'entretien d'embauche

Entretien de Software Development Engineer In Test (SDET) II Bellevue, WA (États-Unis)

how to reverse words in a string preserving the formatting

Répondre

Réponse de l'entretien

2 réponses

0

google it

Candidat à l'entretien, le 11 oct. 2010
0

public void ReverseWords(StringBuilder sb)
        {
            if (sb == null)
            {
                return ;
            }

            String tempStr = "";
            int start = -1;
            int end = -1;

            //"My na"

            for (int i = sb.Length -1; i >= 0; i--)
            {
                if (sb[i] == ' ')
                {
                    if (end != -1)
                    {
                        start = i + 1;
                        // end = 4
                        // start = 3
                        while (start <= end)
                        {
                            tempStr = tempStr + sb[start];
                            start++;
                        }

                        start = -1;
                        end = -1;
                    }

                    tempStr = tempStr + sb[i];
                }

                else
                {
                    if (end == -1)
                    {
                        end = i;
                    }
                }
            }

            if (end != -1)
            {
                for (int i = 0; i <= end; i++)
                {
                    tempStr = tempStr + sb[i];
                }
            }

            Console.WriteLine(tempStr);
        }

Neha, le 24 nov. 2010

Ajouter des réponses ou des commentaires

Pour commenter ceci, se connecter ou s'inscrire