How do you check if a given string is a palindrome?





How do you check if a given string is a palindrome?

Example  

Input : madam
Output : palindrome

Input : madam
Output : Not a palindrom

Solution:

Step’s
·         First do some validation
·         Store the string in a char array
·         Using loop store reverse string in another string
·         Compare the both string


public static void Palindrom(string str)
        {
            if ((str == null) || (str.Length <= 1))
                Console.Write(str);

            else
            {
                str = str.ToUpper();
                char[] chr = str.ToCharArray();
                string str2 = "";
                for (int i = chr.Length - 1; i >= 0; i--)
                {
                    str2 += chr[i];
                }
                if (str.Equals(str2))
                {
                    Console.Write("String " + str + " is palindrom ");
                }
                else
                {
                    Console.Write("String " + str + " is Not a palindrom ");
                }
               
            }

        }


static void Main(string[] args)
        {
            Console.WriteLine("=============================================================================");
            Console.WriteLine("How do you check if a given string is a palindrome?");
            Console.WriteLine("=============================================================================");
            Program objprog = new Program();
            Program.Palindrom("madam");
            Console.ReadLine();


        }

Share:

How can a given string be reversed using recursion?




  How can a given string be reversed using recursion?

Solution:

public static void RevesrseAWord(string str)
        {
            if ((str == null) || (str.Length <= 1))
                Console.Write(str);

            else
            {
                Console.Write(str[str.Length - 1]);
                RevesrseAWord(str.Substring(0, (str.Length - 1)));
            }

           // return newString;
        }

  static void Main(string[] args)
        {
            Console.WriteLine("=============================================================================");
            Console.WriteLine("8. How can a given string be reversed using recursion?");
            Console.WriteLine("=============================================================================");
            Program objprog = new Program();
            Program.RevesrseAWord("ShivKumar");
            Console.ReadLine();

        }


OutPut:

============================================================================
How do you reverse words in a given sentence without using any library method?
=============================================================================
ramuKvihS


Share:

How do you count the occurrence of a given character in a string?



Solution:
We can solve this problem by many way’s
1.     Forloop
2.    Foreach loop
3.    Indexof method
We are doing by indexof method

indexOf() : This method returns the index within this string of the first occurrence of the specified character or -1, if the character does not occur.

public static int AccuranceofChar(string x, char find)
        {
            int i = x.IndexOf(find);
            return i;
        }




        static void Main(string[] args)
        {
            Program objprog = new Program();
            Console.WriteLine("Char Position : "+ Program.AccuranceofChar("ShivKumar", 'v'));
            Console.ReadLine();

        }


OutPut:
Char Position : 3

Share:

Kangaroo jump problem Solution (Hacker Rank)


Kangaroo jump problem Solution (Hacker Rank)











Problem 






Solution 

    static string kangaroo(int x1, int v1, int x2, int v2)
        {
            int kangarro1 = x1 + v1;
            int kangarro2 = x2 + v2;
                while (kangarro1 <= 10000 || kangarro2 >= 10000)
                {
                    if (kangarro1 == kangarro2)
                    {
                        return "YES";

                    }
                    kangarro1 = kangarro1 + v1;
                    kangarro2 = kangarro2 + v2;
                }

                return "NO";
        }


Share:

Apple and Orange Problem Solution (Hacker Rank)






Solution


    static void countApplesAndOranges(int s, int t, int a, int b, int[] apples, int[] oranges)
        {
            int start = s;
            int end = t;
            int appleLoc = a;
            int orangeLoc = b;
            int appleCount = 0;
            int orangeCount = 0;
            int i, locationfall = 0;
            for (i = 0; i <= apples.Length - 1; i++)
            {
                locationfall = apples[i] + appleLoc;
                if (locationfall >= start && locationfall <= end)
                {
                    appleCount++;
                }
            }
            for (i = 0; i <= oranges.Length - 1; i++)
            {
                locationfall = oranges[i] + orangeLoc;
                if (locationfall >= start && locationfall <= end)
                {
                    orangeCount++;
                }
            }
            Console.WriteLine(appleCount);
            Console.WriteLine(orangeCount);
            Console.ReadLine();

        }

Share:

Popular

Tags

Mobile

Recent Posts