Check if all the letters in a string are capital recursively

空扰寡人 提交于 2020-01-05 07:16:27

问题


I have to check if all the letters are capital letters in recursion, and i dont know why this isnt working:

public static bool IsCapital(string str)
    {
        if (str.Length == 1)
            return int.Parse(str[0].ToString()) > 65 && int.Parse(str[0].ToString()) < 90;
        return IsCapital(str.Substring(1)) && int.Parse(str[0].ToString()) > 65 && int.Parse(str[0].ToString()) < 90;
    }

It crashes and says: "Unhandled exception: System.FormatException: Input string was not in a correct format."

Console.WriteLine(IsCapital("abc"));

thanks.


回答1:


To solely address the exception, just don't parse strings. You can directly compare a char to any ushort value.

In other words, this is a valid check (without string parsing)

str[0] > 65

AsciiTable.com should show you why the checks you have will fail on the edges.

Also consider...

  • Characters that aren't letters.
  • IsCapital(null)

Finally, something that might make this easier (assuming non-letters get bypassed) is to create a method along the lines of bool IsNotLowerCase(char c).

Note -- these are all assuming ASCII, as evident by my link.

If you must support full Unicode, hopefully you can use the methods of char.




回答2:


You're attempting to parse a char as an int, instead of casting it to an int.

What you're doing is taking a letter, such as A, and parsing that as an int. A isn't a number in any way, and therefore the parse fails.

What you'll want to do is explicitly cast the char as an int to get the ASCII value you're looking for:

if (str.Length == 1)
{
    return ((int)str[0]) > 65 
        && ((int)str[0]) < 90;
}
return IsCapital(str.Substring(1)) 
    && ((int)str[0]) > 65 
    && ((int)str[0]) < 90;



回答3:


I assume what you're trying to do with int.Parse(str[0].ToString()) is to get the ASCII value.

What you need to use instead is this (int)str[0]

A parse will try to translate the string into a number, so a string that has a value of '412' will get parsed into an int of value 412.




回答4:


I have to check if all the letters are capital letters in recursion

public static bool IsCapital(string str)
{
    if (String.IsNullOrEmpty(str)) return false;
    if (str.Length == 1 &&  char.IsUpper(str[0])) return true;
    return char.IsUpper(str[0]) ? IsCapital(str.Substring(1)) :false;
}



回答5:


I thinks, that number comparison will not work.

First it will return false for string like this "ABC123" even if all letters are capitals. And second there are many national characters that does not fall into range 65..90 even if they are capitals. You should (if you can) use some Char method http://msdn.microsoft.com/en-us/library/d1x97616.aspx




回答6:


public static bool IsCapital(string str)
{
 return !Regex.IsMatch(str, "[a..z]");
}


来源:https://stackoverflow.com/questions/12959883/check-if-all-the-letters-in-a-string-are-capital-recursively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!