samedi 13 novembre 2021

how many time a char have been repeted in a random string

I have this code which is a simple game you pick how many questions and how many characters you want and the program generate a random string of characters and numbers but how do I pick one random character every question and ask the user how many time this char have been repeated for example how many time char A have been repeated in this string: iasfhAjfalkjA answer = 2 times

I don't know If I should put the whole code but I think it helps here is my code

{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Random randomgen = new Random();
            int a; string Sa; int b; string sb;
            int maxanswer = 0;
            int m = 0;
            string question;
            string sss;
            string quit = "QUIT";
            string useranswer;
            string answer ;
            int numofquestions;
            int numofquestionsleft;
            int numofcorrect = 0;
            int numoffalse = 0;

            //ASKING THE USER FOR THE MAX NUMBER OF QUESTIONS WANT TO BE ASKED
            Console.Write("Max Question : ");
            numofquestions = Convert.ToInt32(Console.ReadLine());
            numofquestionsleft = numofquestions;
            //ASKING THE USER TO PUT A NUMBER BETWEEN 3 AND 100 THAT WILL HELP GENERATE A RANDOM string


            Console.WriteLine("enter a value between 3 and 100");
            Sa = Console.ReadLine(); a = Int32.Parse(Sa);

//here is the random generating part
      String sarffsa = "A1a2B3b4C5c6D7d8E9eFfGgHhIiKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"; 
                while (numofquestionsleft > 0 && m <= numofquestions + 1)
                {
                    int length = a;

                String random = "";

                for (int i = 0; i < length; i++)
                {
                    int alphapet = randomgen.Next(42);
                    random = random + sarffsa.ElementAt(alphapet);
                    
                }
                Console.WriteLine("How many time the symbol has been repeted in the follwing characters: {0}", random);

                 answer = Console.ReadLine();

                // adding the question and the user answer and the right answer to an array
                questionlist[m] = random;
                useranswer = Convert.ToString(Console.ReadLine());
                useranswerlist[m] = answer;
                rightanswerslist[m] = answer;

                Console.WriteLine("--------------------------------------------------------");
                // IF USER TYPE QUIT THAT WILL SKIP THE QUESTION AND ADD IT TO WRONG ANSWERS LIST
                if (answer== quit)
                {
                    numofquestionsleft--;
                    ;
                    continue;

                }
              


                numofquestionsleft--;
                //LOOP
                m++;
            }

            while (1 < 2)
            {
                Console.WriteLine(@"TO GET THE NUMBER OF THE RIGHT ANSWERS PRESS 1
TO GET THE NUMBER OF THE WRONG ANSWERS PRESS 2
TO GET THE OPERATION WITH THE MAX NUMBERS OF RIGHT ANSWERS PRESS 3
TO GET THE OPERATION WITH THE MAX NUMBERS OF FALSE ANSWERS PRESS 4
TO VIEW ALL THE QUESTIONS AND YOUR ANSWERS AND CORRECT ANSWERS TYPE 5
TO EXIT TYPE EXIT
");
                sb = Console.ReadLine();

                b = Int32.Parse(sb);
                switch (b)
                {
                    // the number of right answers 
                    case 1:
                        Console.WriteLine("-----------------------------------------------");
                        Console.WriteLine("You have = " + numofcorrect + " Right answers");
                        Console.WriteLine("-----------------------------------------------");
                        break;
                    case 2:
                        // the number of wrong answers
                        Console.WriteLine("-----------------------------------------------");
                        Console.WriteLine("You have = " + numoffalse + " Wrong answers");
                        Console.WriteLine("-----------------------------------------------");
                        break;
                    // Making the rightoperationlist into a group in order and count to show the most frequent array used
                    case 3:
                        var result = (from operation in rightoperationlist
                                      group operation by operation into og
                                      orderby og.Count() descending
                                      select og.Key).FirstOrDefault();


                        Console.WriteLine("RIGHT ANSWER OPERATION = " + result);

                        break;
                    case 4:
                        // Making the Wrongoperationlist into a group in order and count to show the most frequent array used
                        var result02 = (from operation in wrongoperationlist
                                        group operation by operation into og
                                        orderby og.Count() descending
                                        select og.Key).FirstOrDefault();
                        Console.WriteLine("WRONG ANSWER OPERATIONS = " + result02);

                        break;
                    //showing the questions and user answer and the right answer arrays
                    case 5:
                        Console.WriteLine("QUESTIONS               ANSWERS                RIGHTANSWERS");
                        Console.WriteLine("------------------------------------------------------------------------");
                        for (int z = 0; z < numofquestions; z++)
                        {
                            Console.WriteLine(questionlist[z] + "                 " + useranswerlist[z] + "                   " + rightanswerslist[z]);
                        }
                        Console.WriteLine("------------------------------------------------------------------------");
                        break;

                    default:
                        break;

                }

            }
            Console.ReadLine();




        }
    }
}



Aucun commentaire:

Enregistrer un commentaire