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
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();
}
}
}
Comments
Leave a comment