Found 2745 Articles for Csharp

C# program to Reverse words in a string

Samual Sam
Updated on 19-Jun-2020 11:29:21

2K+ Views

Let’s say the following is the string −Hello WorldAfter reversing the string, the words should be visible like −olleH dlroWExampleUse the reverse() method and try the following code to reverse words in a string.Live Demousing System; using System.Linq; class Demo {    static void Main() {       // original string       string str = "Hello World";       // reverse the string       string res = string.Join(" ", str.Split(' ').Select(s => new String(s.Reverse().ToArray())));       Console.WriteLine(res);    } }OutputolleH dlroW

C# program to reverse an array

karthikeya Boyini
Updated on 19-Jun-2020 11:30:41

2K+ Views

Firstly, set the original array −int[] arr = { 15, 16, 17, 18 }; // Original Array Console.WriteLine("Original Array= "); foreach (int i in arr) {    Console.WriteLine(i); }Now, use the Array.reverse() method to reverse the array −Array.Reverse(arr);ExampleThe following is the complete code to reverse an array in C#Live Demousing System; class Demo {    static void Main() {       int[] arr = { 15, 16, 17, 18 };       // Original Array       Console.WriteLine("Original Array= ");       foreach (int i in arr) {          Console.WriteLine(i);       }       // Reverse Array       Array.Reverse(arr);       Console.WriteLine("Reversed Array= ");       foreach (int j in arr) {          Console.WriteLine(j);       }       Console.ReadLine();    } }OutputOriginal Array=   15 16 17 18 Reversed Array=   18 17 16 15

C# program to reverse a string

Samual Sam
Updated on 19-Jun-2020 11:32:09

10K+ Views

Our sample string is −myStr = "Tom";To reverse the string, firstly find the length of the string −// find string length int len; len = myStr.Length - 1;Now, use a while loop until the length is greater than 0 −while (len >= 0) {    rev = rev + myStr[len];    len--; }ExampleYou can try to run the following code to reverse a string in C#.Live Demousing System; class Demo {    static void Main() {       string myStr, rev;       myStr = "Tom";       rev ="";       Console.WriteLine("String is {0}", myStr); ... Read More

C# program to replace all spaces in a string with ‘%20’

karthikeya Boyini
Updated on 19-Jun-2020 11:33:45

2K+ Views

We have a sample string with spaces −str ="Hello World !";Use the Replace() method in C# to replace all spaces in a string with ‘%20’ −str2 = str.Replace(" ", "%20");ExampleYou can try to run the following code to replace all spaces in a string with ‘%20’.Live Demousing System; class Demo {    static void Main() {       String str, str2;       str ="Hello World !";       Console.WriteLine("String: "+str);       str2 = str.Replace(" ", "%20");       Console.WriteLine("String (After replacing): "+str2);    } }OutputString: Hello World ! String (After replacing): Hello%20World%20!

C# program to find common elements in three sorted arrays

Samual Sam
Updated on 28-Jan-2020 08:13:58

174 Views

Firstly, initialize three sorted arrays −int []one = {20, 35, 57, 70}; int []two = {9, 35, 57, 70, 92}; int []three = {25, 35, 55, 57, 67, 70};To find common elements in the three-sorted arrays, iterate through the arrays using a while loop and check the first array with a second and second array with the third −while (i < one.Length && j < two.Length && k < three.Length) {    if (one[i] == two[j] && two[j] == three[k]) {       Console.Write(one[i] + " ");       i++;j++;k++;    }    else if (one[i] < two[j]) ... Read More

C# program to Display Hostname and IP address

karthikeya Boyini
Updated on 19-Jun-2020 11:35:28

977 Views

To find the hostname, use the Dns.GetHostName() method in C# −String hostName = string.Empty; hostName = Dns.GetHostName(); Console.WriteLine("Hostname: "+hostName);Now, use the IPHostEntry.AddressList Property to get IP Address −IPHostEntry myIP = Dns.GetHostEntry(hostName); IPAddress[] address = myIP.AddressList;ExampleTry the following code to display hostname and IP address −using System; using System.Net; class Program {    static void Main() {       String hostName = string.Empty;       hostName = Dns.GetHostName();       Console.WriteLine("Hostname: "+hostName);       IPHostEntry myIP = Dns.GetHostEntry(hostName);       IPAddress[] address = myIP.AddressList;       for (int i = 0; i < address.Length; i++) {          Console.WriteLine("IP Address {1} : ",address[i].ToString());       }       Console.ReadLine();    } }

C# program to find node in Linked List

Samual Sam
Updated on 19-Jun-2020 10:52:04

383 Views

Firstly, create a new linked list −LinkedList myList = new LinkedList();Now add some elements in the linked list −// Add 6 elements in the linked list myList.AddLast("P"); myList.AddLast("Q"); myList.AddLast("R"); myList.AddLast("S"); myList.AddLast("T"); myList.AddLast("U");Let’s now find a node and add a new node after that −LinkedListNode node = myList.Find("R"); myList.AddAfter(node, "ADDED");ExampleYou can try to run the following code to find a node in the linked list.Live Demousing System; using System.Collections.Generic; class Program {    static void Main() {       LinkedList myList = new LinkedList();       // Add 6 elements in the linked list       myList.AddLast("P");   ... Read More

C# program to count upper and lower case characters in a given string

karthikeya Boyini
Updated on 19-Jun-2020 10:52:36

856 Views

To count uppercase characters in a string, check the following condition −myStr[i]>='A' && myStr[i]='a' && myStr[i]

C# Program to find all substrings in a string

Samual Sam
Updated on 19-Jun-2020 10:57:17

1K+ Views

Use the substring() method in C# to find all substrings in a string.Let’s say our string is −XyzLoop through the length of the string and use the Substring function from the beginning to the end of the string −for (int start = 0; start

C# program to display factors of entered number

karthikeya Boyini
Updated on 19-Jun-2020 11:15:47

858 Views

Firstly, enter the number for which you want the factors −Console.WriteLine("Enter the Number:"); n = int.Parse(Console.ReadLine());After that, loop through to find the factors −for (i = 1; i

Advertisements