Answer to Question #179086 in C# for Patrick Ian Kyle Mabunay

Question #179086

Write a program using standard string functions that accepts a price of an item and display its coded value. The base of the key is: X C O M P U T E R S 1 2 3 4 5 6 7 8 9 Sample input/output dialogue: Enter price: 489.50 Coded value: PRS.UX


1
Expert's answer
2021-04-07T06:15:43-0400
using System;

namespace encodingApp
{
    class Program
    {
        static string encoding = "XCOMPUTERS";
        static char separator = '.';

        static bool isValidValue(string input)
        {
            string nums = "0123456789";
            int sepCount = 0;

            foreach (var sym in input)
            {
                if (sepCount > 1) return false;
                if (sym == separator) ++sepCount;
                else if (!nums.Contains(sym + "")) return false;
            }
            return true;
        }

        static string Encode(string price)
        {
            string result = "";
            foreach (var num in price)
                if (num == separator)
                    result += separator;
                else
                    result += encoding[int.Parse(num + "")];
            return result;
        }

        static void Main(string[] args)
        {
            Console.Write("Enter price: ");
            string price = Console.ReadLine();
            if (isValidValue(price))
                Console.WriteLine($"Coded value: {Encode(price)}");
            else
                Console.WriteLine("Invalid input");
            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS