Prefix Game in C#

This is a C# Program to prefix game.

Problem Description

This C# program is used to prefix game.

Problem Solution

Here prefix has to be found for the words which are given.

Program/Source Code

Here is source code of the C# Program to Prefix Game. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Prefix Game
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project
{
    class Program
    {
        static void Main(string[] args)
        {
            string[,] seq = new string[4, 3];
            int row = 0;
            seq[row, 0] = "substring";
            seq[row, 1] = "sub";
            seq[row, 2] = "incorrect";
            row++;
            seq[row, 0] = "input";
            seq[row, 1] = "in";
            seq[row, 2] = "incorrect";
            row++;
            int numrows = 0;
            play(seq, numrows);
            Console.Write("Continue : Press 'y'");
            string next = Console.ReadLine();
            if (next.CompareTo("y") == 0)
            {
                Console.Clear();
                numrows += 2;
                play(seq, numrows);
            }
            Console.ReadLine();
        }
        static void play(string[,] seq, int rows)
        {
                Console.WriteLine("ENGLISH WORD PREFIX GAME");
                for (int i = rows; i <rows+2 ; i++)
                {
                     Console.Write("What is the correct prefix of '{0}':", seq[i, 0]);
                     string ans = Console.ReadLine();
                     if (seq[i, 1].ToLower().CompareTo(ans.ToString().ToLower()) == 0)
                        seq[i, 2] = "correct";
                        Console.WriteLine();
                }
                Console.WriteLine("CHECK YOUR ANSWERS!!!");
                Console.WriteLine("Word\t\t\tPrefix\t\tDescription");
                for (int i = rows; i < rows+2; i++)
                {
                    for (int j = 0; j < 3; j++)
                        Console.Write("{0}\t\t", seq[i, j]);
                    Console.WriteLine();
                }
            Console.Read();
          }
    }
}
Program Explanation

This C# program is used to prefix game. We have already entered the words using seq[] array variable, and perform the play() function by passing the ‘seq’ and ‘numrows’ variable values as an argument.

advertisement

In play() method, using for loop we are reading the English word prefix. Convert the entered word into lowercase using ToLower() method. If condition statement is used to check the value is equal to correct. If the condition is true then execute the iteration of the loop.

Another if condition statement is used to check the value of ‘given’ variable is equal to ‘y’. If the condition is true then execute the statement. Print the prefix has for the words which are given.

Runtime Test Cases
 
ENGLISH WORD PREFIX GAME
What is the correct prefix of 'substring' : sub
What is the correct prefix of 'input' : in
 
CHECK YOUR ANSWERS!!!!
Word       Prefix      Description
substring  sub         correct
input      in          correct

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Free 30-Day Python Certification Bootcamp is Live. Join Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.