A palindrome is any string that is the same when read from start to end or backwards from end to start. For example, radar and solos are both palindromes.
How can code be written to determine if a string is a palindrome as well as count how often a specified letter exists within a string?
The following code determines if a string is a palindrome, but does not obtain the count of a specified character:
namespace oefening_2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Geef een random in: "); //console stel vraag
string sWrd = Console.ReadLine();//console slaagt woord op
Console.WriteLine("geef een random letter in: ");//console stele nog een vraagt
string sletter = Console.ReadLine();//console slaagt letter op
string sWoordOmge = ReverseString(sWrd);
IsPalin(sWrd, sWoordOmge);
Console.ReadLine();
}
//script
public static void IsPalin(string s1, string sWoordOmge)
{
if (s1 == sWoordOmge)
Console.Write("Het {0} is een palindroom", s1);//console geeft antwoord
else
Console.Write("Het {0} is geen palindroom", s1);//console geeft antwoord als het geen palindroom is
}
//berekeningen van console
public static string ReverseString(string s1)
{
string sWoordOmge = "";
for (int i = (s1.Length - 1); i >= 0; i--)
sWoordOmge += s1.Substring(i, 1);
return sWoordOmge;
}
}
}