Questions C# programming help

noBody2k18

Well-known member
  • Jun 23, 2018
    531
    620
    93
    me code eka C# walin gahala dennako

    Write a program that will accept as input a sentence, encode the sentence using the following rules, and then display the sentence in its encoded format. Rules for encoding are as follows:
    • A consonant will be replaced by the 3 rd consonant after it. E.g. G will be replaced by K.
    • A vowel will be replaced by the 2nd vowel before it. E.g. I will be replaced by A.
    • A number in digits will be flipped and doubled. E.g., 52 will be replaced by 50.
    • Full-stops, exclamation marks and question marks will be replaced by the hash sign. Other punctuation marks will be removed. After encoding five sentences the encoded sentences will then be displayed as separate sentences
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    This problem looks easy at the first glance but it is not. A few questions:

    1. What are the constraints? What are the valid characters in the input sentence?
    2. Can the input contain both capital and simple letters? Or only capital letters?
    3. What are the valid punctuation marks for this particular problem?
    4. Can the sentence contain multiple numbers like 5000, 123, 47, so that each number should be flipped and doubled separately?
     
    • Like
    Reactions: noBody2k18

    noBody2k18

    Well-known member
  • Jun 23, 2018
    531
    620
    93
    oyama try karala meeke daanna
    eka line ekak hari kamak ne, code karala danna
    This problem looks easy at the first glance but it is not. A few questions:

    1. What are the constraints? What are the valid characters in the input sentence?
    2. Can the input contain both capital and simple letters? Or only capital letters?
    3. What are the valid punctuation marks for this particular problem?
    4. Can the sentence contain multiple numbers like 5000, 123, 47, so that each number should be flipped and doubled separately?
    Eyaaa yakooo
    Oka had a gannna barida
    Software eng 😂😂😂😂
    Mata neme bro. yaluwek kiwwe hoyala denna kiyala. answer eka hambuna. thanks <3
     

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    I had already coded the solution at the time I made my previous post asking for the constraints so that I could modify my code to suit the constraints. Anyway here is the solution that works for both capital and simple letters and supports multiple numbers within the sentence.

    C#:
    using System;
    using System.Linq;
    
    public class Program
    {
        const int NO_OF_SENTENCES = 5;
    
        static void Main(string[] args)
        {
            string[] sentences = ReadSentences();
            string[] encodedSentences = EncodeSentences(sentences);
    
            WriteEncodedSentences(sentences, encodedSentences);
    
            Console.ReadLine();
        }
    
        static string[] EncodeSentences(string[] sentences)
        {
            string[] encodedSentences = new string[sentences.Length];
    
            for (int i = 0; i < sentences.Length; i++)
                encodedSentences[i] = EncodeSentence(sentences[i]);
    
            return encodedSentences;
        }
    
        static string EncodeSentence(string sentence)
        {
            string encoded = "";
            string number = "";
    
            for (int i = 0; i < sentence.Length; i++)
            {
                if (Char.IsDigit(sentence[i]))
                    number = sentence[i] + number;
                else
                {
                    if (number != "")
                    {
                        encoded += int.Parse(number) * 2;
                        number = "";
                    }
                    encoded += GetEncodedLetter(sentence[i]);
                }
            }
    
            return encoded;
        }
    
        static string GetEncodedLetter(char c)
        {
            string vowels = "AEIOU" + "AEIOU";
            string consonants = "BCDFGHJKLMNPQRSTVWXYZ" + "BCDFGHJKLMNPQRSTVWXYZ";
    
            if (Char.IsLower(c))
            {
                vowels = vowels.ToLower();
                consonants = consonants.ToLower();
            }
    
            if (vowels.Contains(c))
                return "" + vowels[vowels.LastIndexOf(c) - 2];
            else if (consonants.Contains(c))
                return "" + consonants[consonants.IndexOf(c) + 3];
            else if (".!?".Contains(c))
                return "#";
            else if (Char.IsDigit(c))
                return "" + c;
            else if (c == ' ')
                return " ";
            else // Must be some other punctuation mark
                return "";
        }
    
        static string[] ReadSentences()
        {
            string[] sentences = new string[NO_OF_SENTENCES];
    
            for (int i = 0; i < sentences.Length; i++)
            {
                Console.Write("\nInput sentence No. {0}: ", i + 1);
                sentences[i] = Console.ReadLine();
            }
    
            return sentences;
        }
    
        static void WriteEncodedSentences(string[] sentences, string[] encodedSentences)
        {
            Console.WriteLine("\n");
    
            for (int i = 0; i < sentences.Length; i++)
            {
                Console.WriteLine("Sentence: {0}", sentences[i]);
                Console.WriteLine("Encoded Sentence: {0}\n", encodedSentences[i]);
            }
        }
    }

    1.png
     

    noBody2k18

    Well-known member
  • Jun 23, 2018
    531
    620
    93
    I had already coded the solution at the time I made my previous post asking for the constraints so that I could modify my code to suit the constraints. Anyway here is the solution that works for both capital and simple letters and supports multiple numbers within the sentence.

    C#:
    using System;
    using System.Linq;
    
    public class Program
    {
        const int NO_OF_SENTENCES = 5;
    
        static void Main(string[] args)
        {
            string[] sentences = ReadSentences();
            string[] encodedSentences = EncodeSentences(sentences);
    
            WriteEncodedSentences(sentences, encodedSentences);
    
            Console.ReadLine();
        }
    
        static string[] EncodeSentences(string[] sentences)
        {
            string[] encodedSentences = new string[sentences.Length];
    
            for (int i = 0; i < sentences.Length; i++)
                encodedSentences[i] = EncodeSentence(sentences[i]);
    
            return encodedSentences;
        }
    
        static string EncodeSentence(string sentence)
        {
            string encoded = "";
            string number = "";
    
            for (int i = 0; i < sentence.Length; i++)
            {
                if (Char.IsDigit(sentence[i]))
                    number = sentence[i] + number;
                else
                {
                    if (number != "")
                    {
                        encoded += int.Parse(number) * 2;
                        number = "";
                    }
                    encoded += GetEncodedLetter(sentence[i]);
                }
            }
    
            return encoded;
        }
    
        static string GetEncodedLetter(char c)
        {
            string vowels = "AEIOU" + "AEIOU";
            string consonants = "BCDFGHJKLMNPQRSTVWXYZ" + "BCDFGHJKLMNPQRSTVWXYZ";
    
            if (Char.IsLower(c))
            {
                vowels = vowels.ToLower();
                consonants = consonants.ToLower();
            }
    
            if (vowels.Contains(c))
                return "" + vowels[vowels.LastIndexOf(c) - 2];
            else if (consonants.Contains(c))
                return "" + consonants[consonants.IndexOf(c) + 3];
            else if (".!?".Contains(c))
                return "#";
            else if (Char.IsDigit(c))
                return "" + c;
            else if (c == ' ')
                return " ";
            else // Must be some other punctuation mark
                return "";
        }
    
        static string[] ReadSentences()
        {
            string[] sentences = new string[NO_OF_SENTENCES];
    
            for (int i = 0; i < sentences.Length; i++)
            {
                Console.Write("\nInput sentence No. {0}: ", i + 1);
                sentences[i] = Console.ReadLine();
            }
    
            return sentences;
        }
    
        static void WriteEncodedSentences(string[] sentences, string[] encodedSentences)
        {
            Console.WriteLine("\n");
    
            for (int i = 0; i < sentences.Length; i++)
            {
                Console.WriteLine("Sentence: {0}", sentences[i]);
                Console.WriteLine("Encoded Sentence: {0}\n", encodedSentences[i]);
            }
        }
    }

    1.png
    Thank you broooo <3 :love: :love: :love: :love:
     

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,637
    6,949
    113
    United States
    I had already coded the solution at the time I made my previous post asking for the constraints so that I could modify my code to suit the constraints. Anyway here is the solution that works for both capital and simple letters and supports multiple numbers within the sentence.

    C#:
    using System;
    using System.Linq;
    
    public class Program
    {
        const int NO_OF_SENTENCES = 5;
    
        static void Main(string[] args)
        {
            string[] sentences = ReadSentences();
            string[] encodedSentences = EncodeSentences(sentences);
    
            WriteEncodedSentences(sentences, encodedSentences);
    
            Console.ReadLine();
        }
    
        static string[] EncodeSentences(string[] sentences)
        {
            string[] encodedSentences = new string[sentences.Length];
    
            for (int i = 0; i < sentences.Length; i++)
                encodedSentences[i] = EncodeSentence(sentences[i]);
    
            return encodedSentences;
        }
    
        static string EncodeSentence(string sentence)
        {
            string encoded = "";
            string number = "";
    
            for (int i = 0; i < sentence.Length; i++)
            {
                if (Char.IsDigit(sentence[i]))
                    number = sentence[i] + number;
                else
                {
                    if (number != "")
                    {
                        encoded += int.Parse(number) * 2;
                        number = "";
                    }
                    encoded += GetEncodedLetter(sentence[i]);
                }
            }
    
            return encoded;
        }
    
        static string GetEncodedLetter(char c)
        {
            string vowels = "AEIOU" + "AEIOU";
            string consonants = "BCDFGHJKLMNPQRSTVWXYZ" + "BCDFGHJKLMNPQRSTVWXYZ";
    
            if (Char.IsLower(c))
            {
                vowels = vowels.ToLower();
                consonants = consonants.ToLower();
            }
    
            if (vowels.Contains(c))
                return "" + vowels[vowels.LastIndexOf(c) - 2];
            else if (consonants.Contains(c))
                return "" + consonants[consonants.IndexOf(c) + 3];
            else if (".!?".Contains(c))
                return "#";
            else if (Char.IsDigit(c))
                return "" + c;
            else if (c == ' ')
                return " ";
            else // Must be some other punctuation mark
                return "";
        }
    
        static string[] ReadSentences()
        {
            string[] sentences = new string[NO_OF_SENTENCES];
    
            for (int i = 0; i < sentences.Length; i++)
            {
                Console.Write("\nInput sentence No. {0}: ", i + 1);
                sentences[i] = Console.ReadLine();
            }
    
            return sentences;
        }
    
        static void WriteEncodedSentences(string[] sentences, string[] encodedSentences)
        {
            Console.WriteLine("\n");
    
            for (int i = 0; i < sentences.Length; i++)
            {
                Console.WriteLine("Sentence: {0}", sentences[i]);
                Console.WriteLine("Encoded Sentence: {0}\n", encodedSentences[i]);
            }
        }
    }

    1.png

    meka karanne kohomada kiyanna Puluwannda

    When naming your controls use the following convention:
    1. For the first three letters use the typical prefix for the control – e.g. txt
    2. For the next part give a meaningful name – e.g. UserName
    3. For the last part of the control use the underscore followed by the last 2 digits of your registration number – e.g. 24
    4. Your control name should look like “txtUserName_24 Marks will be given for design, proper selection of data types and type conversion, and for proper formatting of output, as well as the versatility of the program.
     
    • Like
    Reactions: noBody2k18

    EKGuest

    Well-known member
  • Nov 16, 2022
    3,206
    5,703
    113
    meka karanne kohomada kiyanna Puluwannda

    When naming your controls use the following convention:
    1. For the first three letters use the typical prefix for the control – e.g. txt
    2. For the next part give a meaningful name – e.g. UserName
    3. For the last part of the control use the underscore followed by the last 2 digits of your registration number – e.g. 24
    4. Your control name should look like “txtUserName_24 Marks will be given for design, proper selection of data types and type conversion, and for proper formatting of output, as well as the versatility of the program.

    Machan I think this description is related to the naming conventions of controls, like btnRegister_24, txtPassword_24 when designing a GUI application. I am not a professional developer and I solve small code puzzles as a hobby, so every program I write is a Console Application. But there are many professional developers in this forum and if your post your full specification in a new thread they will come and help you.
     

    noBody2k18

    Well-known member
  • Jun 23, 2018
    531
    620
    93
    meka karanne kohomada kiyanna Puluwannda

    When naming your controls use the following convention:
    1. For the first three letters use the typical prefix for the control – e.g. txt
    2. For the next part give a meaningful name – e.g. UserName
    3. For the last part of the control use the underscore followed by the last 2 digits of your registration number – e.g. 24
    4. Your control name should look like “txtUserName_24 Marks will be given for design, proper selection of data types and type conversion, and for proper formatting of output, as well as the versatility of the program.
    danne na bro. bump