How to find all possible substring of a given string?


How to find all possible substring of a given string?


  public void PossibleString(String x)
        {
            char[] arr = x.ToCharArray();
            int leng = arr.Length;
            string rev="";
            for (int i = 0; i<= arr.Length-1;  i++)
            {
                rev  = rev + arr[i].ToString();
                Console.Write(rev + " ");
            }
           
         }
        static void Main(string[] args)
        {
            Program p1 = new Program();
         Console.WriteLine(" How to find all possible substring of a given string ? \n");
            p1.PossibleString("shivKumar");
            Console.ReadLine();
        }


OUTPUT:
How to find all possible substring of a given string ?
s sh shi shiv shivK shivKu shivKum shivKuma shivKumar





Share:

How to reverse a string?



How to reverse a string?

public String ReverseAstirng(String x)
        {
            char[] arr = x.ToCharArray();
            int leng = arr.Length;
            string rev="";
            for (int i = arr.Length-1; i >=0 ; i--)
            {
                rev  = rev + arr[i].ToString();
            }
            return rev;
         }
        static void Main(string[] args)
        {
            Program p1 = new Program();
            Console.WriteLine(" How to reverse a string ? ");
            Console.WriteLine("Reverse String = "+ p1.ReverseAstirng("shivKumar"));
            Console.ReadLine();
        }
OutPut:
 How to reverse a string ?
Reverse String = ramuKvihs





Share:

How to count a number of vowels and consonants in a String?


How to count a number of vowels and consonants in a String?

   public String CountVowel(String x)
        {
            x = x.ToLower();
            char[] arr = x.ToCharArray();
            int leng = arr.Length;
            foreach (char ch in arr)
            {
                if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                {
                    count++;
                }
              
            }
            Console.WriteLine("Total Constant " + (leng - count));
            return count.ToString();
          
        }
        static void Main(string[] args)
        {
            Program p1 = new Program();
      Console.WriteLine("How to count a number of vowels and consonants in a String?");

            Console.WriteLine("Total vowel "+ p1.CountVowel("shivKumar"));
            Console.ReadLine();
}

        





Share:

PRINT Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21


PRINT Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21

The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 begins with the numbers 0 and 1 and has the property that each succeeding number is the sum of the two preceding numbers.
Using C#, write a nonrecursive function fibonacci(n) that calculates the nth Fibonacci number. Then write a program to display a table of two columns consisting of the first 15 numbers and their associated Fibonacci numbers.

You can do it by many ways
1st by array
            int[] arr1 = { 0 };
            int[] arr2 = { 1 };
            int val1 = 0;
            int val2=1;
            for (int i = 0; i <= 7; i++)
            {
                if (i < 1)
                    Console.Write(arr1[i] + " " + arr2[i]+" ");
                else
                {
                    int sum = arr1[0] + arr2[0];
                    Console.Write(sum +" ");
                    arr1[0] = arr2[0];
                    arr2[0] = sum;
                }
               
            }
      
 2nd by variable
static void Main(string[] args)
        {
            int[] arr1 = { 0 };
            int[] arr2 = { 1 };
            int val1 = 0;
            int val2=1;
            int sum = 0;
            while (sum <= 21)
            {
                Console.Write(val1 + " ");
                sum= val1 + val2;
                val1 = val2;
                val2 = sum;

            }
                  
            Console.ReadLine();
        }

If you want n number of Fibonacci series

In Above Example you can pass dynamic value  

            while (sum <= “you can pass the number here”)


Share:

How to Find out Given 2 String is anagram or Not ?

 Anagram

How to Find out Given 2 String is anagram or Not

Anagram string are those string which character are same in no of sequence
For Example
Required input and output
Input: silent
Output: listen
Input: Ganga
Output: agnga
Input: Yahoo
Output: hooya
Solution:

You can achieve this by many ways 

1.     Using of char Array then sort the array & compare the array char value

Step 1:
public bool strignAnagram(String str1, String S2)
        {
                     
            char[] Str1Arr= str1.ToCharArray();
            char[] Str2Arr = S2.ToCharArray();
           
            Array.Sort(Str1Arr);
            Array.Sort(Str2Arr);

            for (int i = 0; i <= str1.Length - 1; i++)
            {
                if (Str1Arr[i] != Str2Arr[i])
                {
                    return false;
                }
            }
            return true;
        }


















In Main Function
Console.WriteLine("STring Anagram = "+ obj.strignAnagram("silent", "tnelis"));


 How to Find out Given 2 String is anagram or Not?

Share:

Popular

Tags

Mobile

Recent Posts