Simple Symbols

版权声明:本文为博主原创文章,采用“署名-非商业性使用-禁止演绎 2.5 中国大陆”授权。欢迎转载,但请注明作者姓名和文章出处。 https://blog.csdn.net/njit_77/article/details/79747174

Challenge

Using the C# language, have the function  SimpleSymbols(str) take the  str parameter being passed and determine if it is an acceptable sequence by either returning the string  true or  false. The  str parameter will be composed of  + and  = symbols with several letters between them ( ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a  +symbol. So the string to the left would be false. The string will not be empty and will have at least one letter. 
Sample Test Cases

Input:"+d+=3=+s+"

Output:"true"


Input:"f++d+"

Output:"false"


Simple  Symbols算法判断字符串里面的字母是否被'+'包围;true-是;false-不是

        public static string SimpleSymbols(string str)
        {
            char[] chArray = str.ToArray();
            int length = chArray.Length;
            char ch = '\0';
            for (int i = 0; i < length; i++)
            {
                ch = chArray[i];

                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                {
                    if (i == 0 || i == length - 1)
                    {
                        return "false";
                    }
                    else if (chArray[i - 1] != '+' || chArray[i + 1] != '+')
                    {
                        return "false";
                    }
                }
            }
            return "true";
        }



猜你喜欢

转载自blog.csdn.net/njit_77/article/details/79747174