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
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
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
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
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
Bitwise operator works on bits and performs bit by bit operation.The Bitwise operators supported by C# are listed in the following table. Assume variable A holds 60 and variable B holds 13 −OperatorDescriptionExample&Bitwise AND Operator copies a bit to the result if it exists in both operands.(A & B) = 12, which is 0000 1100|Bitwise OR Operator copies a bit if it exists in either operand.(A | B) = 61, which is 0011 1101^Bitwise XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) = 49, which is 0011 0001~Bitwise One's Complement Operator ... Read More
The most basic way to track down errors is by turning on error information in your browser. By default, Internet Explorer shows an error icon in the status bar when an error occurs on the page. Double-clicking this icon takes you to a dialog box showing information about the specific error that occurred. Since this icon is easy to overlook, Internet Explorer gives you the option to automatically show the Error dialog box whenever an error occurs. To enable this option, select Tools → Internet Options → Advanced tab. and then finally check the "Display a Notification About Every Script Error" box option as ... Read More
The following is an example of Single Inheritance in C#. In the example, the base class is Father and declared like the following code snippet −class Father { public void Display() { Console.WriteLine("Display"); } }Our derived class is Son and is declared below −class Son : Father { public void DisplayOne() { Console.WriteLine("DisplayOne"); } }ExampleThe following is the complete example to implement Single Inheritance in C#.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyAplication { class Demo { static void Main(string[] args) { ... Read More
Multilevel Inheritance occurs when a derived class is formed from another derived class.Grandfather, father, and son are the perfect example to represent Multilevel Inheritance in C# −ExampleThe following is an example stating the usage of multilevel inheritance in C#.Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Son : Father { public void DisplayTwo() { Console.WriteLine("Son.. "); } static void Main(string[] args) { Son s = new Son(); s.Display(); s.DisplayOne(); ... Read More
To calculate factorial in C#, you can use while loop and loop through until the number is not equal to 1.Here n is the value for which you want the factorial −int res = 1; while (n != 1) { res = res * n; n = n - 1; }Above, let’s say we want 5! (5 factorial)For that, n=5, Loop Iteration 1 −n=5 res = res*n i.e res =5;Loop Iteration 2 −n=4 res = res*n i.e. res = 5*4 = 20Loop Iteration 3 −n=3 res = res*n i.e. res = 20*3 = 60ExampleIn this way, all the ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP