Home > database >  Frequency in array but with Substrings
Frequency in array but with Substrings

Time:05-20

I'm trying to solve an exercise, which I will write here:

Italian cards are a set of 40. Each of them has a "number" (a(ace),2,3,4,5,6,7,d,c,r) and a type (d,c,b,s). The program must ask as input 10 cards as a string of two characters (one number and one type). example: ac, 2s, 6b, rd... Each card must be memorized in an array. Additionally, impossible cards are forbidden by the program (example: 9p, kw ecc.), you shouldn't be able to insert more than 10 cards. The program must display how many aces are present in the 10 input cards. Bonus: the program must ask you in the end what number of card you wish to know the frequency of, and then display what you requested (for example if there are 4d, 4c and 4b among the 10 cards, the program must return "3", if I ask how many 4 are there).

Now, I managed to solve almost all requests, but I can't solve the last one. A generalized frequency of the number of cards.

This is what I have written. I know it probably is absolutely not efficient, please don't laugh.

Searching this site, I found this way to calculate and display the frequency of the elements of an array (Count the frequency of element of an array in C#), but I can't really figure out how to adapt it to my case with strings and substrings.

Any help would be much appreciated.

using System;

namespace esercizio3
{
public class Program
{


    
    static void Main(string[] args)
    {

    int ctr;     
    
    string[] arr = new string[10];
    Console.WriteLine("Insert 10 cards as a string of two characters: number (A, 2, 3, …, 7, D, C, R) and type (D, C, B, S)");
    Console.Write("insert the first card: ");
    arr[0] = Console.ReadLine();
    string a00 = arr[0].Substring(0,1);   
    string a01 = arr[0].Substring(1,1);
    string a02 = arr[0].Substring(0,2);
        if (a00 != "a" && a00 != "2" && a00 != "3" && a00 != "4" && a00 != "5" && a00 != "6" && a00 != "7" && a00 != "d" && a00 != "c" && a00 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a01 != "d" && a01 != "c" && a01 != "b" && a01 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a02 == "dd" || a02 == "cc") {
            throw new Exception("please type the cards correctly");
        }

    Console.Write("insert the second card: ");
    arr[1] = Console.ReadLine();
    string a10 = arr[1].Substring(0,1);   
    string a11 = arr[1].Substring(1,1);
    string a12 = arr[1].Substring(0,2);
        if (a10 != "a" && a10 != "2" && a10 != "3" && a10 != "4" && a10 != "5" && a10 != "6" && a10 != "7" && a10 != "d" && a10 != "c" && a10 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a11 != "d" && a11 != "c" && a11 != "b" && a11 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a12 == "dd" || a12 == "cc") {
            throw new Exception("please type the cards correctly");
        }

    Console.Write("insert the third card: ");
    arr[2] = Console.ReadLine();
    string a20 = arr[2].Substring(0,1);   
    string a21 = arr[2].Substring(1,1);
    string a22 = arr[2].Substring(0,2);
        if (a20 != "a" && a20 != "2" && a20 != "3" && a20 != "4" && a20 != "5" && a20 != "6" && a20 != "7" && a20 != "d" && a20 != "c" && a20 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a21 != "d" && a21 != "c" && a21 != "b" && a21 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a22 == "dd" || a22 == "cc") {
            throw new Exception("please type the cards correctly");
        }

    Console.Write("insert the fourth card: ");
    arr[3] = Console.ReadLine();
    string a30 = arr[3].Substring(0,1);   
    string a31 = arr[3].Substring(1,1);
    string a32 = arr[3].Substring(0,2);
        if (a30 != "a" && a30 != "2" && a30 != "3" && a30 != "4" && a30 != "5" && a30 != "6" && a30 != "7" && a30 != "d" && a30 != "c" && a30 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a31 != "d" && a31 != "c" && a31 != "b" && a31 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a32 == "dd" || a32 == "cc") {
            throw new Exception("please type the cards correctly");
        }


    Console.Write("insert the fifth card: ");
    arr[4] = Console.ReadLine();
    string a40 = arr[4].Substring(0,1);   
    string a41 = arr[4].Substring(1,1);
    string a42 = arr[4].Substring(0,2);
        if (a40 != "a" && a40 != "2" && a40 != "3" && a40 != "4" && a40 != "5" && a40 != "6" && a40 != "7" && a40 != "d" && a40 != "c" && a40 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a41 != "d" && a41 != "c" && a41 != "b" && a41 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a42 == "dd" || a42 == "cc") {
            throw new Exception("please type the cards correctly");
        }

    Console.Write("insert the sixth card: ");
    arr[5] = Console.ReadLine();
    string a50 = arr[5].Substring(0,1);   
    string a51 = arr[5].Substring(1,1);
    string a52 = arr[5].Substring(0,2);
        if (a50 != "a" && a50 != "2" && a50 != "3" && a50 != "4" && a50 != "5" && a50 != "6" && a50 != "7" && a50 != "d" && a50 != "c" && a50 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a51 != "d" && a51 != "c" && a51 != "b" && a51 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a52 == "dd" || a52 == "cc") {
            throw new Exception("please type the cards correctly");
        }


    Console.Write("insert the seventh card: ");
    arr[6] = Console.ReadLine();
    string a60 = arr[6].Substring(0,1);   
    string a61 = arr[6].Substring(1,1);
    string a62 = arr[6].Substring(0,2);
        if (a60 != "a" && a60 != "2" && a60 != "3" && a60 != "4" && a60 != "5" && a60 != "6" && a60 != "7" && a60 != "d" && a60 != "c" && a60 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a61 != "d" && a61 != "c" && a61 != "b" && a61 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a62 == "dd" || a62 == "cc") {
            throw new Exception("please type the cards correctly");
        }


    Console.Write("insert the eigth card: ");
    arr[7] = Console.ReadLine();
    string a70 = arr[7].Substring(0,1);   
    string a71 = arr[7].Substring(1,1);
    string a72 = arr[7].Substring(0,2);
        if (a70 != "a" && a70 != "2" && a70 != "3" && a70 != "4" && a70 != "5" && a70 != "6" && a70 != "7" && a70 != "d" && a70 != "c" && a70 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a71 != "d" && a71 != "c" && a71 != "b" && a71 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a72 == "dd" || a72 == "cc") {
            throw new Exception("please type the cards correctly");
        }
    
    Console.Write("insert the ninth card: ");
    arr[8] = Console.ReadLine();
    string a80 = arr[8].Substring(0,1);   
    string a81 = arr[8].Substring(1,1);
    string a82 = arr[8].Substring(0,2);
        if (a80 != "a" && a80 != "2" && a80 != "3" && a80 != "4" && a80 != "5" && a80 != "6" && a80 != "7" && a80 != "d" && a80 != "c" && a80 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a81 != "d" && a81 != "c" && a81 != "b" && a81 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a82 == "dd" || a82 == "cc") {
            throw new Exception("please type the cards correctly");
        }

    Console.Write("insert the tenth card: ");
    arr[9] = Console.ReadLine();
    string a90 = arr[9].Substring(0,1);   
    string a91 = arr[9].Substring(1,1);
    string a92 = arr[9].Substring(0,2);
        if (a90 != "a" && a90 != "2" && a90 != "3" && a90 != "4" && a90 != "5" && a90 != "6" && a90 != "7" && a90 != "d" && a90 != "c" && a90 != "r") {
            throw new Exception("please type the cards correctly");
        }
        if (a91 != "d" && a91 != "c" && a91 != "b" && a91 != "s") {
            throw new Exception("please type the cards correctly");
        }
        if (a92 == "dd" || a92 == "cc") {
            throw new Exception("please type the cards correctly");
        }

    for (int i = 0; i<10; i  ) {
        Console.WriteLine(arr[i]);
    }
    ctr = 0;

    foreach (string i in arr) {
    
    int StringPosition = i.IndexOf("a");
    if (StringPosition == 0) {
        ctr  ;
    }
    
    }
   Console.WriteLine($"The number of aces is: {ctr}");

}

}

}

CodePudding user response:

If the only thing you want to know is how to get the count of Aces you can do this:

Console.WriteLine("The number of aces is: {0}", arr.Count(s => s[0] == 'a'));

Strings are just character arrays, so getting the first character in the array and comparing to "a" will do the trick.

CodePudding user response:

For anyone wondering, I found a solution. Adding this method:

   static int search(string[]array, string s)
{
        int counter = 0;
        for (int j = 0; j < 10; j  )

            if (s.Equals(array[j].Substring(0,1)))
                counter  ;
            return counter;
    
}
  • Related