Found 34488 Articles for Programming

C# program to check for a string that contains all vowels

karthikeya Boyini
Updated on 22-Jun-2020 12:55:59

386 Views

To check for all vowels, firstly set the condition to check −string res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();Above, we have used the string −string str = "the quick brown fox jumps over the lazy dog";Now, using the Any() method checks whether the string has any vowels or not −if(!res.Any()) Console.WriteLine("No vowels!");Loop through the string to get the vowels −Example Live Demousing System; using System.Linq; public class Program {    public static void Main() {       string str = "the quick brown fox jumps over the lazy dog";       var res = str.Where(chk =< "aeiouAEIOU".Contains(chk)).Distinct();     ... Read More

C# program to multiply all numbers in the list

Samual Sam
Updated on 22-Jun-2020 12:56:44

812 Views

Firstly, set the list −List myList = new List () {    5,    10,    7 };Now, set the value of a variable to 1 that would help us in multiplying −int prod = 1;Loop through and get the product −foreach(int i in myList) {    prod = prod*i; }The following is the code −Example Live Demousing System; using System.Collections.Generic; public class Program {    public static void Main() {       List myList = new List() {          5,          10,          7       };       Console.WriteLine("List: ");       foreach(int i in myList) {          Console.WriteLine(i);       }       int prod = 1;       foreach(int i in myList) {          prod = prod*i;       }       Console.WriteLine("Product: {0}",prod);    } }OutputList: 5 10 7 Product: 350

Foreach in C++ and C#

karthikeya Boyini
Updated on 22-Jun-2020 12:57:28

556 Views

Foreach in C++C++ 11 introduced foreach loop to traverse over each element. Here is an example −Example Live Demo#include using namespace std; int main() {    int myArr[] = { 99, 15, 67 };    // foreach loop    for (int ele : myArr)    cout

Print first m multiples of n in C#

Samual Sam
Updated on 30-Jul-2019 22:30:23

541 Views

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

Print number with commas as 1000 separators in C#

Giri Raju
Updated on 22-Jun-2020 12:57:50

1K+ Views

Firstly, set the number as string −string num = "1000000.8765";Now, work around differently for number before and after the decimal −string withoutDecimals = num.Substring(0, num.IndexOf(".")); string withDecimals = num.Substring(num.IndexOf("."));Use the ToString() method to set the format for 1000 separators −ToString("#,##0")The following is the complete code to display number with commas as 1000 separators −Example Live Demousing System; public class Program {    public static void Main() {       string num = "1000000.8765";       string withoutDecimals = num.Substring(0, num.IndexOf("."));       string withDecimals = num.Substring(num.IndexOf("."));       withoutDecimals = Convert.ToInt32(withoutDecimals).ToString("#,##0");       Console.WriteLine(withoutDecimals + withDecimals);    } }Output1,000,000.8765

Print first letter of each word in a string in C#

usharani
Updated on 22-Jun-2020 12:58:16

1K+ Views

Let’s say the string is −string str = "Never Give Up!";Firstly, split each word −string[] strSplit = str.Split();Now, loop through each word and use the substring method to display the first letter as shown in the following code −Example Live Demousing System; public class Program {    public static void Main() {       string str = "Never Give Up!";       Console.WriteLine("Initial String= "+str);       Console.WriteLine("Displaying first letter of each word...");       string[] strSplit = str.Split();       foreach (string res in strSplit) {          Console.Write(res.Substring(0,1));       }    } }OutputInitial String= Never Give Up! Displaying first letter of each word... NGU

Intersect Method in C#

mkotla
Updated on 22-Jun-2020 12:58:45

371 Views

Use the Intesect method to get the common elements −Create lists −var list1 = new List{99, 87}; var list2 = new List{56, 87, 45, 99};Now, use the Intersect() method to get the common elements from the above list −list1.Intersect(list2);Here is the complete code −Example Live Demousing System.Collections.Generic; using System.Linq; using System; public class Demo {    public static void Main() {           // two lists       var list1 = new List{99, 87};       var list2 = new List{56, 87, 45, 99};       // common values       var res = list1.Intersect(list2);       foreach(int i in res) {          Console.WriteLine(i);       }    } }Output99 87

Print first letter of each word in a string using C# regex

seetha
Updated on 22-Jun-2020 12:48:42

340 Views

Let’s say our string is −string str = "The Shape of Water got an Oscar Award!";Use the following Regular Expression to display first letter of each word −@"\b[a-zA-Z]"Here is the complete code −Example Live Demousing 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);          foreach (Match m in mc) {             Console.WriteLine(m);          }       } ... Read More

C# Equivalent to Java's Double Brace Initialization?

usharani
Updated on 22-Jun-2020 12:49:01

238 Views

Java’s Double Brace Initialization does the same work what a single brace can achieve in C#.Double Brace creates and initialize objects in a single Java expression.Let’s say the following is in Java −ExampleList list = new List() {{    add("One");    add("Two");    add("Three");    add("Four"); }}The same you can use for Collection Initializer in C# as −List list = new List() {"One","Two", “Three”, “Four”};

C# Equivalent to Java Functional Interfaces

mkotla
Updated on 22-Jun-2020 12:50:16

2K+ Views

Equivalent of Java’s Functional Interfaces in C# is Delegates.Let us see the implementation of functional interface in Java −Example@FunctionalInterface public interface MyInterface {    void invoke(); } public class Demo {    void method(){       MyInterface x = () -> MyFunc ();       x.invoke();    }    void MyFunc() {    } }The same implementation in C# delagates −Examplepublic delegate void MyInterface (); public class Demo {    internal virtual void method() {       MyInterface x = () => MyFunc ();       x();    }    internal virtual void MyFunc() {    } }

Advertisements