Is Alternate Cases, Checks to see if a string has alternate cases - CSharp System

CSharp examples for System:String Case

Description

Is Alternate Cases, Checks to see if a string has alternate cases

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;//from  www .  ja va 2 s . c o m

public class Main{
        //Checks to see if a string has alternate cases
        //lOnGsTrInG -> True
        public static bool IsAlternateCases(string input)
        {
            if (input.Length <= 1) return false;

            bool lastIsUpper = String.Compare(input.Substring(0, 1), input.Substring(0, 1).ToUpper(), false) == 0;

            for (int i = 1; i < input.Length; i++)
            {
                if (lastIsUpper)
                {
                    if (String.Compare(input.Substring(i, 1), input.Substring(i, 1).ToLower(), false) != 0)
                        return false;
                }
                else
                {
                    if (String.Compare(input.Substring(i, 1), input.Substring(i, 1).ToUpper(), false) != 0)
                        return false;
                }

                lastIsUpper = !lastIsUpper;
            }

            return true;
        }
}

Related Tutorials