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.