String program #5
(Input/read
character)
Enter any character: a
Entered character is: a
The program for the string program is
written in C# programming language and will accept a character as input. Based
on the entry of character, it returns the output. We can input/read character
in 2 ways:
1.
Automatically
postback after reading an input
2.
Reading
first character from the input string
Let’s find out a simple and easy way
to code the program. But, before that let’s understand what we are doing in this
article. We are learning how to input/read a character. There are 2 ways in
doing that:
1.
This
is the best way to read/input a character. It automatically postback after
reading an input, you do not need to submit the value. We are using the Keychar
attribute of ReadKey() method to read character. Console.ReadKey().KeyChar
2.
But
if your requirement is like - the user can type any string but we only want the
first character from the input string then you can use the 2nd way. We
are using the Read() method (char)Console.Read()
Practical Implementation:
using System;
namespace patternProblem.String
{
class String5 //input/read character
{
static void Main(string[] args)
{
//How to read/input a character?
//Best way to read/input a character. It automatically
postback after reading an input, you donot need to submit the value.
Console.Write("Enter any character: ");
char
ch = Console.ReadKey().KeyChar;
Console.WriteLine();
Console.WriteLine("Entered character is: " + ch);
Console.WriteLine();
//But if your requirement is like- the user can type any
string but we only want the first character from the input string then you can
use
Console.Write("Enter any character: ");
char ch1 = (char)Console.Read();
Console.WriteLine("Entered character is: " + ch1);
Console.WriteLine();
Console.ReadKey();
}
}
}
Output:
The input character
here is ‘a’. So, program will return the input character as input. The output
is shown below:
Enter any character: a
Entered character is: a
Press any key to continue . . .
Some Other
Facts:
There are 3 other facts which can also
be used to input/read character.
o
We
all know that to read string on console application we use Console.ReadLine() method
o
But
if we use the above method to read/input character it work fine when input
string is of length 1
o
But
if its length is greater than 1 it throw run-time exception. Unhandled Exception:
System.FormatException: String must be exactly one character long.
Practical Implementation:
using System;
namespace patternProblem.String
{
class String5 //input/read character
{
static void Main(string[] args)
{
//How to read/input a character?
//Some other facts
//1. We all know that to read string on console
application we use ReadLine() mehtod
Console.Write("Enter string: ");
string str = Console.ReadLine();
Console.WriteLine("Entered string is: " + str);
Console.WriteLine();
//2. But if we use the above method to read/input
character it work fine when input string is of length 1
Console.Write("Enter string: ");
char
ch2 = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Entered character is: " + ch2);
Console.WriteLine();
//3. But if it's length is greater than 1 it throw
run-time exception
//Unhandled Exception: System.FormatException: String must
be exactly one character long.
Console.Write("Enter string: ");
char
ch3 = Convert.ToChar(Console.ReadLine());
Console.WriteLine("Entered character is: " + ch3);
Console.WriteLine();
Console.ReadKey();
}
}
}
Output:
The input character
here is ‘a’. So, program will return the input character as input. The output
is shown below:
Enter any character: a
Entered character is: a
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Keep learning and sharing...
No comments:
Post a Comment