Found 34488 Articles for Programming

Find frequency of each word in a string in C#

Samual Sam
Updated on 22-Jun-2020 10:14:23

627 Views

To find the frequency of each word in a string, you can try to run the following code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    }    public static void Main() {       String s = "Football!";       int []cal = new int[maxCHARS];       calculate(s, cal);       for (int i = 0; i < maxCHARS; i++)       if(cal[i] > ... Read More

How to find the size of a variable without using sizeof in C#?

Syed Javed
Updated on 23-Jun-2020 08:17:47

2K+ Views

To get the size of a variable, sizeof is used.int x; x = sizeof(int);To get the size of a variable, without using the sizeof, try the following code −// without using sizeof byte[] dataBytes = BitConverter.GetBytes(x); int d = dataBytes.Length;Here is the complete code.Example Live Demousing System; class Demo {    public static void Main() {       int x;       // using sizeof       x = sizeof(int);       Console.WriteLine(x);       // without using sizeof       byte[] dataBytes = BitConverter.GetBytes(x);       int d = dataBytes.Length;       Console.WriteLine(d);    } }Output4 4

Declare a const array in C#

Ankith Reddy
Updated on 22-Jun-2020 09:57:59

8K+ Views

In C#, use readonly to declare a const array.public static readonly string[] a = { "Car", "Motorbike", "Cab" };In readonly, you can set the value at runtime as well unlike const.Another alternative of achieving what we saw above −public ReadOnlyCollection a { get { return new List { "Car", "Motorbike", "Cab" }.AsReadOnly();}} .NET framework 4.5 brings an improvement to what we saw −public ReadOnlyCollection a { get; } = new ReadOnlyCollection( new string[] { "Car", "Motorbike", "Cab" } );

How to find whether the Number is Divisible by 2 using C#?

George John
Updated on 22-Jun-2020 09:59:28

201 Views

To find whether a number is divisibe by 2 is not, check what happens when the number is divisible by 2.If the remainder is 0, then the number is divisible by 2, else it is false −if (num % 2 == 0) {    Console.WriteLine("Divisible by 2 "); } else {    Console.WriteLine("Not divisible by 2"); }The following is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo {    class Test {       static void Main(string[] args) {          int num;          num = 18; ... Read More

C# program to find the most frequent element

karthikeya Boyini
Updated on 22-Jun-2020 09:58:31

782 Views

Let’s say our string is −String s = "HeathLedger!";Now create a new array.int []cal = new int[maxCHARS];Create a new method and pass both the string and the new array in it. Find the maximum occurrence of a character.static void calculate(String s, int[] cal) {    for (int i = 0; i < s.Length; i++)    cal[s[i]]++; }Let us see the complete code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    } ... Read More

How to find the size of a list in C#?

Chandu yadav
Updated on 22-Jun-2020 09:59:59

2K+ Views

Declare and initialize a list.var products = new List (); products.Add("Accessories"); products.Add("Clothing"); products.Add("Footwear");To get the size, use the Capacity property.products.CapacityNow let us see the complete code to find the size of a list.Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       var products = new List ();       products.Add("Accessories");       products.Add("Clothing");       products.Add("Footwear");       Console.WriteLine("Our list....");       foreach(var p in products) {          Console.WriteLine(p);       }       Console.WriteLine("Size of list = " + products.Capacity);    } }OutputOur list.... Accessories Clothing Footwear Size of list = 4

How to find Volume and Surface Area of a Sphere using C#?

Samual Sam
Updated on 22-Jun-2020 10:00:28

235 Views

For volume and surface area of a sphere, firstly declare a variable with the value of radius.int r = 15;Get the volume f the sphere.// calculating volume of sphere cal_volume = (4.0 / 3) * (22 / 7) * r * r * r;Now the surface area of the sphere is calculated −cal_area = 4 * (22 / 7) * r * r;Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(string[] args) {       double cal_area, cal_volume, r;       // radius       ... Read More

How to find the product of 2 numbers using recursion in C#?

Arjun Thakur
Updated on 22-Jun-2020 10:04:30

174 Views

Firstly, set the two numbers to be multiplied.val1 = 10; val2 = 20;Now cal the method to find the product.product(val1, val2);Under the product method, a recursive call will get you the product.val1 + product(val1, val2 – 1)Let us see the complete code to find the product of 2 numbers using recusion.Exampleusing System; class Calculation {    public static void Main() {       int val1, val2, res;       // the two numbers       val1 = 10;       val2 = 20;       // finding product       Demo d = new ... Read More

How to find the Rank of a given Array in C#?

karthikeya Boyini
Updated on 22-Jun-2020 10:03:27

184 Views

To find the rank of an array, use the Rank property.Firstly, declare and initialize an array.int[,] myArr = new int[3,3];Now, get the rank.myArr.RankLet us see the complete code −Example Live Demousing System; class Demo {    static void Main() {       int[,] myArr = new int[3,3];       Console.WriteLine("Rank of Array : " + myArr.Rank);    } }OutputRank of Array : 2

How to find the index of an item in a C# list in a single step?

Samual Sam
Updated on 09-Sep-2023 23:28:21

35K+ Views

To get the index of an item in a single line, use the FindIndex() and Contains() methods.int index = myList.FindIndex(a => a.Contains("Tennis"));Above, we got the index of an element using the FindIndex(), which is assisted by Contains() method for that specific element.Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List () {          "Football",          "Soccer",          "Tennis",       };       // finding index   ... Read More

Advertisements