| |

Question: Please provide the program code and output in C# language for the following:

Please provide the program code and output in C# language for the following:

1. Given a string, return the count of the number of times that a substring length 2 appears in the string and also as the last 2 chars of the string, so “hixxxhi” yields 1 (we won’t count the end substring).

CountLast2(“hixxhi”) -> 1
CountLast2(“xaxxaxaxx”) -> 1
CountLast2(“axxxaaxx”) -> 2

public int CountLast2(string str) {

}

2, Given an array of ints, return the number of 9’s in the array.

Count9({1, 2, 9}) -> 1
Count9({1, 9, 9}) -> 2
Count9({1, 9, 9, 3, 9}) -> 3public int Count9 (int[] numbers) {}

3. Given an array of ints, return true if one of the first 4 elements in the array is a 9. The array length may be less than 4.

ArrayFront9({1, 2, 9, 3, 4}) -> true
ArrayFront9({1, 2, 3, 4, 9}) -> false
ArrayFront9({1, 2, 3, 4, 5}) -> false
public boolean ArrayFront9(int[] numbers) {}

Transcribed text:

Expert Chegg Question Answer:

free chegg question answer
Smart Teacher From Answerie.comhttps://answerie.com
Answer:
The code and output for the sample given in the question are provided. The code is well commented as well so that you could easily understand it.

Please leave an upvote :)

CODE:

using System;

public class Program
{
        
        public static int countLast2(string str)
        {
                int n = str.Length;
                int res = 0;
                for(int i = 1; i < n - 2; i++)
                {
                        //we check all the pairs of adjacent characters with 
                        //the last 2 and if they match we increase the res
                        if(str[i] == str[n-1] && str[i-1] == str[n-2])
                                res++;
                }
                return res;
        }
        
        public static int Count9 (int[] numbers) {
                
                int n = numbers.Length;
                int res = 0;
                //iteratre over all the elements of the array
                //and count the numbers of 9s
                for(int i = 0; i < n; i++)
                {
                        if(numbers[i] == 9) res++;
                }
                return res;
        }
        
        public static Boolean ArrayFront9(int[] numbers) {
                
                int n = numbers.Length;
                //we will check only the first 4 or all the elements of the array
                //whichever is minimum
                for(int i = 0; i < Math.Min(n, 4); i++)
                {
                        //if 9 is among them, we return true
                        if(numbers[i] == 9)
                                return true;
                }
                //else we return false
                return false;
        }
        
    public static void Main()
    {
                //Running the samples given in the question
        Console.WriteLine(countLast2("hixxhi"));
        Console.WriteLine(countLast2("xaxxaxaxx"));
                Console.WriteLine(countLast2("axxxaaxx"));
                Console.Write("\n");
                
                Console.WriteLine(Count9(new int[] {1, 2, 9}));
                Console.WriteLine(Count9(new int[] {1, 9, 9}));
                Console.WriteLine(Count9(new int[] {1, 9, 9, 3, 9}));
                Console.Write("\n");
                
                Console.WriteLine(ArrayFront9(new int[] {1, 2, 9, 3, 4}));
                Console.WriteLine(ArrayFront9(new int[] {1, 2, 3, 4, 9}));
                Console.WriteLine(ArrayFront9(new int[] {1, 2, 3, 4, 5}));
    }
}
SAMPLE RUN:

Faq:
*** 

Free Chegg Question Answer

Leave a Reply

Your email address will not be published. Required fields are marked *