Function IsAlphabetic Learn programming Java

Lesson:

Functions


Exercise:

Function IsAlphabetic 17


Objetive:

Create a function that tells if a character is alphabetic (A through Z) or not. It should be used like this:

if (IsAlphabetic ("a"))
System.Console.WriteLine ("It is an alphabetic character");

(Note: do not worry about accents and ñ)


Code:

public class Main
{
	public static boolean IsAlpha(char simbolo)
	{
		if ((simbolo >= 'a') && (simbolo <= 'z'))
		{
			return true;
		}

		if ((simbolo >= 'A') && (simbolo <= 'Z'))
		{
			return true;
		}

		return false;
	}

	public static boolean IsAlpha2(char simbolo)
	{
		simbolo = Character.toLowerCase(simbolo);
		if ((simbolo >= 'a') && (simbolo <= 'z'))
		{
			return true;
		}

		return false;
	}

	public static boolean IsAlpha3(char simbolo)
	{
		simbolo = String.valueOf(simbolo).toLowerCase().charAt(0);
		if ((simbolo >= 'a') && (simbolo <= 'z'))
		{
			return true;
		}

		return false;
	}

	public static boolean IsAlpha4(char simbolo)
	{
		String validas = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqwrstuvwxyz";
		if (validas.contains(simbolo))
		{
			return true;
		}

		return false;
	}

	public static void main(String[] args)
	{
		System.out.println(IsAlpha('w'));
		System.out.println(IsAlpha('2'));
	}
}