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";
}