-1

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;
        }
    }
}
Bob Bryan
  • 3,687
  • 1
  • 32
  • 45
jacouille98
  • 23
  • 1
  • 3
  • the exercise is "make a programe where the console ask you a word and a letter, the console calculates if the word is a palindrome and also counts how many times, the letter that was given up, is in the word, exemple: console ask word: hello, console ask letter: "e", console answerrs " word is not a palindrome and the letter "e" is 1 time in the word", above is how to find if the word is a palindrome but i don't know how to make the letter counter – jacouille98 Mar 20 '17 at 09:43
  • First convert your post in **Simple English** so that we can see what you've written – J.SMTBCJ15 Mar 20 '17 at 09:47
  • The palindrome part seems to be fine. For the other, just use loop through the string and count the given letter – Alexandru Pupsa Mar 20 '17 at 09:49
  • that palindrome part is fine, it's just that i'm kinda new to this and i don't know how to make the second part – jacouille98 Mar 20 '17 at 15:35
  • Modified the title to improve English grammar and make it more clear. Moved the details of the question to the body and explained what a palindrome is. Changed the name WoordOmkeren (which is Dutch for invert word) to ReverseString. – Bob Bryan Mar 23 '17 at 05:30

1 Answers1

0

If you are going to be asking questions on this site, then you will need to work harder on using English only. Mixing some English with Dutch does not work too well.

Another option to reverse a string is this code:

public static string ReverseString( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

The problem with your reverse string method is that it creates a new string with each iteration - which is very inefficient for large strings. If you need to write a method that shows the mechanics of reversing a string, then another option would be to use the same logic in your method, but to use a StringBuilder object instead and then simply return the final string from StringBuilder's ToString method.

Here is my full solution to the problem:

public static void TestPalin()
{
   // This method prompts the user for a word and a letter.
   // It then determines whether or not the word is a palindrome and the number of times the letter appears in the palindrome.
   //
   Console.WriteLine("Enter a word: ");
   string word = Console.ReadLine(); // Read in the word from the console.

   Console.WriteLine("Enter a letter: ");
   char letter = Console.ReadLine()[0]; // Read in the specified letter from the console.

   int letterCount;
   string reverseWord = ReverseAndGetCharCount(word, letter, out letterCount);
   IsPalin(word, reverseWord, letter, letterCount);
   Console.ReadLine();
}

public static void IsPalin(string word, string reverseWord, char letter, int letterCount)
{
   // This method is used to display the result of the TestPalin method.
   //
   if (word == reverseWord)
      Console.WriteLine($"The word {word} is a palindrome");
   else
      Console.WriteLine($"The word {word} is not a palindrome");
   Console.WriteLine($"The letter {letter} appeared {letterCount} times within the word");
}

public static string ReverseAndGetCharCount(string s1, char selectedChar, out int countChar)
{
   // This method reverses a string and counts the number of times the selected letter appears in the word.
   //
   char[] charArray = s1.ToCharArray();
   char[] reverseArray = new char[s1.Length];

   countChar = 0;
   int j = 0;
   char c;
   for (int i = s1.Length - 1; i >= 0; i--)
   {
      c = charArray[i];
      reverseArray[j++] = c;
      if (c == selectedChar)
         countChar++;
   }
   return new string(reverseArray);
}

Style wise, you should know that starting a variable with the type of class (also known as Hungarian notation) is out of vogue. So, variable names like sWrd should be simply named word instead.

You should get in the habit of writing a comment at the beginning of each method to give a brief description of what that method does. Don't describe how a method works in the comment, unless it is very complicated. Just briefly describe in a line or two what it is supposed to do.

Lastly, starting with C# 6, strings now have the capability to use string interpolation, which is indicated with the $ character. This produces clearer code with less typing.

Community
  • 1
  • 1
Bob Bryan
  • 3,687
  • 1
  • 32
  • 45