none
Program check if the value is number, sign (?,.!) or vowel (a,e,i,o,u) RRS feed

  • Question

  • So I need to write a program to check if the value is number, sign or vowel. *I'm new to this, it's a homework and it's been pretty hard for me

    SO far i've been to here:                                    

     {
            static void Main(string[] args)
            {
    int a;
                Console.WriteLine("Symbol: ");
                a = int.Parse(Console.ReadLine());

                string value = "123.3";
                double num;
                if (!double.TryParse(value, out num))
                    throw new InvalidOperationException("That's not a number.");
                }

    }

    Tuesday, February 26, 2019 11:38 AM

All replies

  • In order to check that it is a number:

    Console.WriteLine( "Symbol: " );

    string text = Console.ReadLine();

    double num;

    if( double.TryParse( text, out num ) )

    {

        Console.WriteLine( "That's a number." );

    }

    else

    {

        // other checks

        // . . .

    }

     

    Inside the else block you can add verifications for signs and vowels.

    Tuesday, February 26, 2019 4:21 PM
  • Hi

    In addition to Viorel response , below is the entire code to check if string is numeric/sign/vowel.

    //Check numeric
            string value = "1te?";
            double num;
            if (double.TryParse(value, out num))
            {
                Console.WriteLine("That's a number" + "<br/>");
            }
            else
            {
                Console.WriteLine("String is non numeric" + "<br/>");
            }
    
    //Vowel Checker
            char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
            char[] ch = value.ToCharArray();
            foreach (char c in ch)
            {
                if (vowels.Contains(c))
                {
                    Console.WriteLine(c + " is an vowel word" + "<br/>");
                }
                else
                {
                    Console.WriteLine(c + " not an vowel word" + "<br/>");
                }
            }
    
    //Special Sign
    //Note: You will have to decalre "using System.Text.RegularExpressions" namespace 
    
    
           if (System.Text.RegularExpressions.Regex.IsMatch(value, "[^a-z0-9]"))
            {
                Console.WriteLine("String has special Sign");
            }
            else
            {
                Console.WriteLine("No Special Sign");
            }



    Thanks

    • Edited by ME_77 Wednesday, February 27, 2019 5:52 AM
    • Proposed as answer by ME_77 Wednesday, February 27, 2019 5:52 AM
    Wednesday, February 27, 2019 5:50 AM
  • Hi, 

    Thank you for posting here.

    For your question, you want to write a program to check if the value is number, sign or vowel.

    According to Viorel_’s solution, I make a simple and complete code.

    You could try the following code.

    static void Main(string[] args)
            {
                Console.WriteLine("Symbol: ");
    
                string text = Console.ReadLine();
    
                double num;
                    if (double.TryParse(text, out num))
                    {
                        Console.WriteLine("That's a number.");
    
                    }
                    else
                    {
                        string specialChar = @"\|!#$%&/()=?»«@£§€{}.-;'<>_,";
                        foreach (char item in specialChar)
                        {
                            if(text==item.ToString())
                            {
                                Console.WriteLine("That's a Symbol");
                            }
                        }                  
                      char a = Convert.ToChar(text);
                        
                        if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
                        {
                            Console.WriteLine("That's a vowel");
                        }
    
                    }
                Console.ReadKey();
            }

    Result:

    Hope my solution could be helpful.

    Best regards,

    Jack

     


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Wednesday, February 27, 2019 6:17 AM
    Moderator