C # -- character operations,

Source: Internet
Author: User

C # -- character operations,

Question requirements: A random string consisting of letters and numbers. When a user continuously enters the string 'hello', the program ends the user input and displays the number of letters and numbers entered by the user.

Code:

Using System; using System. collections. generic; using System. linq; using System. text; namespace character operation {public class Program {public static void Main () {char s = '#'; int LetterIndex = 0, DigitIndex = 0; Console. write ("enter a string (when hello is input):"); turn: if (s! = 'H') {if (char. isLetter (s) LetterIndex ++; if (char. isDigit (s) DigitIndex ++; s = Console. readKey (). keyChar;} if (s = 'H') {LetterIndex ++; s = Console. readKey (). keyChar; if (s = 'E') {LetterIndex ++; s = Console. readKey (). keyChar; if (s = 'l') {LetterIndex ++; s = Console. readKey (). keyChar; if (s = 'l') {LetterIndex ++; s = Console. readKey (). keyChar; if (s = 'O') {LetterIndex ++; Console. writeLine ("\ n has {0} Letters and {1} digits. ", LetterIndex, DigitIndex); Console. writeLine ("press any key to end. "); Console. readKey ();} else goto turn ;}}}

Question Analysis: First, this question requires the user to end the input when entering the string "hello". It is better to judge the five letters separately. Second, the program needs to automatically end the input, and we need to use the Console. readKey (). keyChar automatically reads one character entered by the user each time.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.