Found 2628 Articles for Csharp

C# Program to Convert Character case

karthikeya Boyini
Updated on 19-Jun-2020 09:01:03

391 Views

Let’s say your string is −str = "AMIT";To convert the above uppercase string in lowercase, use the ToLower() method −Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());ExampleThe following is the code in C# to convert character case.Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {          string str;          str = "AMIT";          Console.WriteLine("UpperCase : {0}", str);          // convert to lowercase          Console.WriteLine("Converted to LowerCase : {0}", str.ToLower());          Console.ReadLine();       }    } }OutputUpperCase : AMIT Converted to LowerCase : amit

C# Program to Convert Binary to Decimal

Samual Sam
Updated on 19-Jun-2020 09:01:36

3K+ Views

Firstly, set the binary value −int num = 101;Now assign the binary to a new variable −binVal = num;Till the value is greater than 0, loop through the binary number and base value like this, while (num > 0) {    rem = num % 10;    decVal = decVal + rem * baseVal;    num = num / 10;    baseVal = baseVal * 2; }ExampleThe following is the code to convert binary to decimal.Live Demousing System; using System.Collections.Generic; using System.Text; namespace Demo {    class MyApplication {       static void Main(string[] args) {       ... Read More

C# program to convert binary string to Integer

karthikeya Boyini
Updated on 19-Jun-2020 09:02:11

755 Views

Use the Convert.ToInt32 class to fulfill your purpose of converting a binary string to an integer.Let’s say our binary string is −string str = "1001";Now each char is parsed −try {    //Parse each char of the passed string    val = Int32.Parse(str1[i].ToString());    if (val == 1)       result += (int) Math.Pow(2, str1.Length - 1 - i);    else if (val > 1)       throw new Exception("Invalid!"); } catch {    throw new Exception("Invalid!"); }Check the above for each character in the passed string i.e. “100” using a for a loop. Find the length of ... Read More

C# program to count occurrences of a word in string

Samual Sam
Updated on 19-Jun-2020 09:02:48

3K+ Views

Set the string first −string str = "Hello World! Hello!";Now check the string for the occurrences of a word “Hello’ and loop through −while ((a = str1.IndexOf(pattern, a)) != -1) {    a += pattern.Length;    count++; }ExampleYou can try to run the following code to count occurrences of a word in a string.Live Demousing System; class Program {    static void Main() {       string str = "Hello World! Hello!";       Console.WriteLine("Occurrence:"+Check.CheckOccurrences(str, "Hello"));    } } public static class Check {    public static int CheckOccurrences(string str1, string pattern) {       int count ... Read More

C# program to count the number of words in a string

karthikeya Boyini
Updated on 19-Jun-2020 09:03:23

6K+ Views

Let us first declare the string −string str = "Hello World!";Now loop through the complete string and find the whitespace or tab or newline character −while (a

C# program to convert a list of characters into a string

Samual Sam
Updated on 19-Jun-2020 09:04:00

681 Views

Firstly, declare the character array and set the value of each character −char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o';Now, use the string class constructor and create a new string from the above array of characters −string myChar = new string(ch);ExampleLet us see the code to convert a list of characters to string in C#.Live Demousing System; namespace Demo {    class MyApplication {       static void Main(string[] args) {          char[] ch = new char[5];          ch[0] = ... Read More

C# Program to Check Whether the Entered Number is an Armstrong Number or Not

karthikeya Boyini
Updated on 19-Jun-2020 09:04:36

880 Views

For an Armstrong number, let us say a number has 3 digits, then the sum of cube of its digits is equal to the number itself.For example, 153 is equal to −1³ + 3³ + 5³To check for it using C#, check the value and find its remainder. Here “val” is the number you want to check for Armstrong −for (int i = val; i > 0; i = i / 10) {    rem = i % 10;    sum = sum + rem*rem*rem; }Now compare the addition with the actual value. If it matches, that would mean the ... Read More

C# program to check password validity

Samual Sam
Updated on 19-Jun-2020 09:05:00

746 Views

While creating a password, you may have seen the validation requirements on a website like a password should be strong and have −Min 8 char and max 14 charOne lower caseNo white spaceOne upper caseOne special charLet us see how to check the conditions one by one −Min 8 char and max 14 charif (passwd.Length < 8 || passwd.Length > 14) return false;Atleast one lower caseif (!passwd.Any(char.IsLower)) return false;No white spaceif (passwd.Contains(" ")) return false;One upper caseif (!passwd.Any(char.IsUpper)) return false;Check for one special characterstring specialCh = @"%!@#$%^&*()?/>.

C# program to check if a string is palindrome or not

karthikeya Boyini
Updated on 19-Jun-2020 09:05:35

11K+ Views

To check if a string is palindrome or not, you need to first find the reverse of the string using −Array.reverse()After that use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.ExampleLet us try the complete example. Here, our string is “Malayalam”, which is when reversed gives the same result.Live Demousing System; namespace palindromecheck {    class Program {       static void Main(string[] args) {          string string1, rev;          string1 = "Malayalam";          char[] ... Read More

C# program to check if a string contains any special character

Samual Sam
Updated on 19-Jun-2020 09:06:09

9K+ Views

To check if a string contains any special character, you need to use the following method −Char.IsLetterOrDigitUse it inside for loop and check or the string that has special characters.Let us say our string is −string str = "Amit$#%";Now convert the string into character array −str.ToCharArray();With that, use a for loop and to check for each character using the isLetterOrDigit() method.ExampleLet us see the complete code.Live Demousing System; namespace Demo {    class myApplication {       static void Main(string[] args) {          string str = "Amit$#%";          char[] one = str.ToCharArray();   ... Read More

Advertisements