Write a function in CSharp that accepts a credit card number. It should returns a string where all the characters are hidden with an asterisk except the last four.
Md Masum Kazi
Here’s a C# function that accepts a credit card number and returns a string where all characters are replaced with asterisks except for the last four sprunki digits:using System;
public class CreditCardHider{ public static string HideCreditCardNumber(string creditCardNumber) { if (string.IsNullOrEmpty(creditCardNumber)) { throw new ArgumentException(“Credit card number cannot be null or empty.”); }
// Get the length of the credit card number int length = creditCardNumber.Length; // Check if the number has at least 4 digits if (length < 4) { throw new ArgumentException("Credit card number must have at least 4 digits."); } // Create a string of asterisks for all but the last four characters string hiddenPart = new string('*', length - 4); string visiblePart = creditCardNumber.Substring(length - 4); // Combine and return the result return hiddenPart + visiblePart;}public static void Main(string[] args){ string creditCard = "1234567812345678"; string hiddenCard = HideCreditCardNumber(creditCard); Console.WriteLine(hiddenCard); // Output: ************5678}
// Get the length of the credit card number
int length = creditCardNumber.Length;
// Check if the number has at least 4 digits
if (length < 4)
{
throw new ArgumentException("Credit card number must have at least 4 digits.");
}
// Create a string of asterisks for all but the last four characters
string hiddenPart = new string('*', length - 4);
string visiblePart = creditCardNumber.Substring(length - 4);
// Combine and return the result
return hiddenPart + visiblePart;
public static void Main(string[] args)
string creditCard = "1234567812345678";
string hiddenCard = HideCreditCardNumber(creditCard);
Console.WriteLine(hiddenCard); // Output: ************5678