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
-
Economics & Finance
Csharp Articles
Page 85 of 196
C# program to count the occurrences of each character
Counting character occurrences in a string is a common programming task in C#. This can be accomplished using various approaches, from basic loops to modern LINQ methods and dictionary-based solutions. Using Basic Loop with String Manipulation The first approach uses a while loop and string replacement to count occurrences − using System; public class Demo { public static void Main() { string str = "Website"; Console.WriteLine("String: " + str); ...
Read MoreC# program to count total set bits in a number
A set bit refers to a bit that has a value of 1 in the binary representation of a number. Counting set bits is a common programming problem that involves examining each bit position to determine how many are set to 1. For example, the number 11 in decimal has the binary representation 1011, which contains 3 set bits (three 1s). Approach The most straightforward approach uses bitwise operations to examine each bit − Use the bitwise AND operator (&) with 1 to check if the least significant bit is set Right-shift ...
Read MoreC# Program to display name of Current Thread
To display the name of the current thread in C#, use the Name property of the Thread.CurrentThread object. This property allows you to both set and retrieve the name of the currently executing thread. Syntax Following is the syntax to get the current thread − Thread thread = Thread.CurrentThread; Following is the syntax to set and get the thread name − thread.Name = "ThreadName"; string currentName = thread.Name; Using Thread.CurrentThread Property The Thread.CurrentThread property returns a reference to the currently executing thread. You can then use the Name property ...
Read MoreC# Program to display priority of Thread
In C#, thread priority determines how the operating system schedules threads for execution. The Priority property of the Thread class allows you to both view and set the priority of a thread. Thread priority is represented by the ThreadPriority enumeration. Syntax To get the current thread and display its priority − Thread thread = Thread.CurrentThread; ThreadPriority priority = thread.Priority; To set a thread's priority − thread.Priority = ThreadPriority.High; ThreadPriority Enumeration Values Priority Level Description Lowest The thread has the lowest priority ...
Read MoreC# program to display factors of entered number
A factor of a number is any integer that divides the number evenly without leaving a remainder. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12 because each of these numbers divides 12 exactly. To find factors of a number in C#, we use the modulus operator (%) to check if the remainder is zero when dividing the number by potential factors. Algorithm The algorithm to find factors involves the following steps − Start with 1 and iterate through all numbers up to the given number For each number, check ...
Read MoreC# program to count upper and lower case characters in a given string
To count uppercase and lowercase characters in a string, we can iterate through each character and check if it falls within specific ASCII ranges. For uppercase letters, we check if the character is between 'A' and 'Z', while for lowercase letters, we check if it's between 'a' and 'z'. Syntax Following is the syntax for checking uppercase characters − if (myStr[i] >= 'A' && myStr[i] = 'a' && myStr[i] = 'a' && myStr[i] = 'A' && myStr[i]
Read MoreC# program to find node in Linked List
The LinkedList class in C# provides methods to search for nodes within the list. The Find() method returns the first LinkedListNode that contains the specified value, or null if the value is not found. Syntax Following is the syntax for finding a node in a LinkedList − LinkedListNode node = linkedList.Find(value); Following is the syntax for finding the last occurrence of a node − LinkedListNode node = linkedList.FindLast(value); Return Value The Find() method returns a LinkedListNode object containing the value, or null if the value is not found. The ...
Read MoreC# program to Display Hostname and IP address
To find the hostname of the current machine, use the Dns.GetHostName() method in C#. This method returns the host name of the local computer as a string. To get the IP addresses, use the IPHostEntry.AddressList property which provides an array of all IP addresses associated with the hostname. Syntax Following is the syntax for getting the hostname − string hostName = Dns.GetHostName(); Following is the syntax for getting IP addresses − IPHostEntry hostEntry = Dns.GetHostEntry(hostName); IPAddress[] addresses = hostEntry.AddressList; Example The following code demonstrates how to display hostname and ...
Read MoreC# program to replace all spaces in a string with '%20'
When working with strings in C#, you may need to replace spaces with '%20' for URL encoding or other formatting purposes. The Replace() method provides a simple way to substitute all occurrences of a character or substring with another value. Syntax Following is the syntax for using the Replace() method − string newString = originalString.Replace(oldValue, newValue); Parameters oldValue − The string or character to be replaced newValue − The string or character to replace with Return Value The method returns a new string with all occurrences of the specified ...
Read MoreC# program to Reverse words in a string
In C#, reversing words in a string means reversing each individual word while keeping the words in their original positions. For example, "Hello World" becomes "olleH dlroW" where each word is reversed but the word order remains the same. Syntax Using LINQ with Split(), Select(), and Reverse() methods − string result = string.Join(" ", str.Split(' ').Select(word => new string(word.Reverse().ToArray()))); Using a traditional loop approach − string[] words = str.Split(' '); for (int i = 0; i < words.Length; i++) { words[i] = ReverseWord(words[i]); } string result = string.Join(" ...
Read More