Found 34488 Articles for Programming

C# program to convert binary string to Integer

karthikeya Boyini
Updated on 19-Jun-2020 09:02:11

763 Views

Use the Convert.ToInt32 class to fulfill your purpose of converting a binary string to an integer.Let’s say our binary string is −string str = "1001";Now each char is parsed −try {    //Parse each char of the passed string    val = Int32.Parse(str1[i].ToString());    if (val == 1)       result += (int) Math.Pow(2, str1.Length - 1 - i);    else if (val > 1)       throw new Exception("Invalid!"); } catch {    throw new Exception("Invalid!"); }Check the above for each character in the passed string i.e. “100” using a for a loop. Find the length of ... Read More

C# program to count occurrences of a word in string

Samual Sam
Updated on 19-Jun-2020 09:02:48

3K+ Views

Set the string first −string str = "Hello World! Hello!";Now check the string for the occurrences of a word “Hello’ and loop through −while ((a = str1.IndexOf(pattern, a)) != -1) {    a += pattern.Length;    count++; }ExampleYou can try to run the following code to count occurrences of a word in a string.Live Demousing System; class Program {    static void Main() {       string str = "Hello World! Hello!";       Console.WriteLine("Occurrence:"+Check.CheckOccurrences(str, "Hello"));    } } public static class Check {    public static int CheckOccurrences(string str1, string pattern) {       int count ... Read More

C# program to count the number of words in a string

karthikeya Boyini
Updated on 19-Jun-2020 09:03:23

6K+ Views

Let us first declare the string −string str = "Hello World!";Now loop through the complete string and find the whitespace or tab or newline character −while (a

C# program to convert a list of characters into a string

Samual Sam
Updated on 19-Jun-2020 09:04:00

683 Views

Firstly, declare the character array and set the value of each character −char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o';Now, use the string class constructor and create a new string from the above array of characters −string myChar = new string(ch);ExampleLet us see the code to convert a list of characters to string in C#.Live Demousing System; namespace Demo {    class MyApplication {       static void Main(string[] args) {          char[] ch = new char[5];          ch[0] = ... Read More

C# Program to Check Whether the Entered Number is an Armstrong Number or Not

karthikeya Boyini
Updated on 19-Jun-2020 09:04:36

885 Views

For an Armstrong number, let us say a number has 3 digits, then the sum of cube of its digits is equal to the number itself.For example, 153 is equal to −1³ + 3³ + 5³To check for it using C#, check the value and find its remainder. Here “val” is the number you want to check for Armstrong −for (int i = val; i > 0; i = i / 10) {    rem = i % 10;    sum = sum + rem*rem*rem; }Now compare the addition with the actual value. If it matches, that would mean the ... Read More

C# program to check password validity

Samual Sam
Updated on 19-Jun-2020 09:05:00

748 Views

While creating a password, you may have seen the validation requirements on a website like a password should be strong and have −Min 8 char and max 14 charOne lower caseNo white spaceOne upper caseOne special charLet us see how to check the conditions one by one −Min 8 char and max 14 charif (passwd.Length < 8 || passwd.Length > 14) return false;Atleast one lower caseif (!passwd.Any(char.IsLower)) return false;No white spaceif (passwd.Contains(" ")) return false;One upper caseif (!passwd.Any(char.IsUpper)) return false;Check for one special characterstring specialCh = @"%!@#$%^&*()?/>.

C# program to check if a string is palindrome or not

karthikeya Boyini
Updated on 19-Jun-2020 09:05:35

11K+ Views

To check if a string is palindrome or not, you need to first find the reverse of the string using −Array.reverse()After that use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.ExampleLet us try the complete example. Here, our string is “Malayalam”, which is when reversed gives the same result.Live Demousing System; namespace palindromecheck {    class Program {       static void Main(string[] args) {          string string1, rev;          string1 = "Malayalam";          char[] ... Read More

C# program to check if a string contains any special character

Samual Sam
Updated on 19-Jun-2020 09:06:09

9K+ Views

To check if a string contains any special character, you need to use the following method −Char.IsLetterOrDigitUse it inside for loop and check or the string that has special characters.Let us say our string is −string str = "Amit$#%";Now convert the string into character array −str.ToCharArray();With that, use a for loop and to check for each character using the isLetterOrDigit() method.ExampleLet us see the complete code.Live Demousing System; namespace Demo {    class myApplication {       static void Main(string[] args) {          string str = "Amit$#%";          char[] one = str.ToCharArray();   ... Read More

C# Program to check if a number is prime or not

karthikeya Boyini
Updated on 10-Sep-2023 08:06:41

43K+ Views

To calculate whether a number is prime or not, we have used a for loop. Within that on every iteration, we use an if statement to find that the remainder is equal to 0, between the number itself.for (int i = 1; i

C# program to check for URL in a String

Samual Sam
Updated on 19-Jun-2020 08:53:45

2K+ Views

Use the StartWith() method in C# to check for URL in a String.Let us say our input string is −string input = "https://example.com/new.html";Now we need to check for www or non-www link. For this, use the if statement in C# −if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) { }ExampleYou can try to run the following code to check for URL in a string.Live Demousing System; class Demo {    static void Main() {       string input = "https://example.com/new.html";       // See if input matches one of these starts.       if (input.StartsWith("https://www.example.com") || input.StartsWith("https://example.com")) {       ... Read More

Advertisements