Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Csharp Articles
Page 2 of 196
How to find a matching substring using regular expression in C#?
Our string is − string str = " My make "; Use the following regular expression to find the substring “make” @"\bmake\b" Here is the complete code − Example using System; using System.Text.RegularExpressions; namespace RegExApplication { public class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); ...
Read MorePrint first m multiples of n in C#
To print m multiples of n, first set the value of m and n − int n = 6, m = 1; Now loop through the value of m, increment it and multiply with n on every iteration − while (m
Read MoreHow to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?
We need to take three-pointers, low, mid, high. We will use low and mid pointers at the start, and the high pointer will point at the end of the given array.If array [mid] =0, then swap array [mid] with array [low] and increment both pointers once.If array [mid] = 1, then no swapping is required. Increment mid pointer once.If array [mid] = 2, then we swap array [mid] with array [high] and decrement the high pointer once.Time complexity − O(N)Exampleusing System; namespace ConsoleApplication{ public class Arrays{ private void Swap(int[] arr, int pos1, int pos2){ ...
Read MoreHow to rotate an array k time using C#?
Given an array and number k, the problem states that we have to rotate the array k times.If the given number is 3 then the array must be rotated 3 times.Create a function reverse which takes the array, start and end as a parameter.In the 1st step call reverse method from 0 to array length.In the 2nd step call the reverse method from 0 to k-1.In the 3rd step call the reverse method from k+1 to array length.Exampleusing System; namespace ConsoleApplication{ public class Arrays{ public void ReverseArrayKTimes(int[] arr, int k){ Reverse(arr, 0, ...
Read MoreHow to return the index of first unique character without inbuilt functions using C#?
Create an empty new array of length 256, traverse through the entire string character by character and increment the value in the new array. At the end traverse the entire array and return the first character that has value 1.Example 1aabccd -→2 1 2 1 → Return the first character which is having count 1. That is b.Example 2using System; namespace ConsoleApplication{ public class Arrays{ public int ReturnIndexOfFirstUniqueCharachter(string s){ int index = -1; int[] arrayValues = new int[256]; for (int i = 0; i ...
Read MoreHow to find the missing number and the repeated number in a sorted array without using any inbuilt functions using C#?
To find the missing numberCreate a new array and traverse through the entire array and make the number true in the new array if the number is found Traverse through the entire array and return the first false element as the missing element.To find the repeating elementThe first true element from the new array will be the repeated element.Exampleusing System; namespace ConsoleApplication{ public class Arrays{ public void MissingNumberAndRepeatedNumber(int[] arr){ bool[] tempArray = new bool[arr.Length + 1]; int missingelement = -1; int repeatingelement = -1; ...
Read MoreWhat are the different ways to find missing numbers in a sorted array without any inbuilt functions using C#?
There are three methods as written below −In the first methodUse the formula n(n+1)/2 that counts the number of elements and then need to subtract from the elements in the array.In the second methodCreate a new array and traverse through the entire array and make the number false whichever is found.In the third methodUse Xor operation. which gives the missing number.Exampleusing System; namespace ConsoleApplication{ public class Arrays{ public int MissingNumber1(int[] arr){ int totalcount = 0; for (int i = 0; i < arr.Length; i++){ ...
Read MoreHow to find the minimum number of jumps required to reach the end of the array using C#?
We can simply start from the first element and repeatedly call for all the elements reachable from first element. The minimum number of jumps to reach end from first can be calculated using minimum number of jumps needed to reach end from the elements reachable from first.Array == {1, 3, 6, 3, 2, 3, 6, 8, 9, 5};Number of steps required is 4Exampleusing System; namespace ConsoleApplication{ public class Arrays{ public int MinJumps(int[] arr, int l, int h){ if (h == l) return 0; if (arr[l] == 0) return int.MaxValue; int min = int.MaxValue; for (int i = l + 1; i
Read MoreHow to rotate a matrix of size n*n to 90-degree k times using C#?
The entire matrix needs to be rotated k number of times. In a matrix there is a total of n/2 squares in n*n matrix and we can process each square one at a time using nested loop. In each square, elements are moving in a cycle of 4 elements then we swap the elements involved in an anticlockwise direction for each cycle.Element at position (n-1-j, i) will go to position(i, j)Element at position (i, j) will go to position(j, n-1-i)Element at position (j, n-1-i) will go to position(n-1-i, n-1-j)Element at position (n-1-i, n-1-j) will go to position(n-1-j, i)Exampleusing System; using ...
Read MoreHow to print a matrix of size n*n in spiral order using C#?
To rotate a matrix in spiral order, we need to do following until all the inner matrix and the outer matrix are covered −Step1 − Move elements of top rowStep2 − Move elements of last columnStep3 − Move elements of bottom rowStep4 − Move elements of first columnStep5 − Repeat above steps for inner ring while there is an inner matrixExampleusing System; namespace ConsoleApplication{ public class Matrix{ public void PrintMatrixInSpiralOrder(int m, int n, int[, ] a){ int i, k = 0, l = 0; while (k < m ...
Read More