Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

C# Program to find number of occurence from a character in string

Hi, Below program is for getting number of occurrence from a given string.    

 class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter String: ");
            string input = Console.ReadLine();
            while (input.Length > 0)
            {
                Console.Write(input[0] + " : ");
                int count = 0;
                for (int j = 0; j < input.Length; j++)
                {
                    if (input[0] == input[j])
                    {
                        count++;
                    }
                }
                Console.WriteLine(count);
                input = input.Replace(input[0].ToString(), string.Empty); //Remove matched character
            }
            Console.ReadLine();
        }
    }

Output:
Enter String: csharpprogram
c : 1
s : 1
h : 1
a : 2
r : 3
p : 2
o : 1
g : 1
m : 1

Program for get total no of alphabets,digits and special char from given string

Hello,

Below program is for get total count of alphabets,digits and special char from given string.
Hope it will be useful for you.

 class Program
    {
        static void Main(string[] args)
        {
            string s = "";
            int alphacount = 0, digitcount = 0, specialcount = 0;
            Console.WriteLine("Enter string");
            s = Console.ReadLine();
            foreach (int i in s)
            {

                if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122))
                    alphacount++;
                else if (i >= 48 && i <= 57)
                    digitcount++;
                else
                    specialcount++;
            }
            Console.WriteLine("Total no of Alphabets:"+""+ alphacount);
            Console.WriteLine("Total no of Digits:" + "" + digitcount);
            Console.WriteLine("Total no of Specialchar:" + "" + specialcount);
            Console.ReadLine();
        }
    }

  For ex input:goswami himanshu15@gmail.com :
  Output:Total no of Alphabets:23
               Total no of Digits:2
               Total no of Specialchar:3

    

Program for check that given string is palindrome or not.

Hello,
Below program is for check whether given string is palindrome or not.means that string that you have provide and reverse of that string should match.
Hope you will enjoy it.

 //check given string is palindrome or not.
    class Program
    {
        static void Main(string[] args)
        {
            string s = "";
            Console.WriteLine("Enter string");
            s = Console.ReadLine();
            string final = "";
            for (int i = s.Count()-1; i >= 0; i--)
            {
               final += s[i];
            }
            if (final == s)
            {
                Console.WriteLine("Given string is palindrome");
                Console.Write(final);
            }
            else
            {
                Console.WriteLine("Given string is not palindrome");
                Console.Write(final);

            }
            Console.ReadLine();
        }
    }

   if input="madam"
   output:"Given string is palindrome".

Display inserted string in reverse without using inbuilt function.

Hello,

Below program is for display inserted string in reverse. Hope you will enjoy.

//Display string in Reverse
    class Program
    {
        static void Main(string[] args)
        {
            string s = "";
            Console.WriteLine("Enter string");
            s = Console.ReadLine();
            string final = "";
            for (int i = s.Count()-1; i >= 0; i--)
            {
               final += s[i];
            }
            Console.Write(final);
            Console.ReadLine();
        }
    }

   If input="Hello Program"
   Output: margorP olleH

Remove duplicate character from string without using inbuilt function.

Hello,
Below program is for remove duplicate characters from given string. you can see that I have given string name as "hemanshu goswami". in that string duplicate characters are like 'h','m','a','s' and etc. so at the end you will be see output as "hemansu gowi". means that if any duplicate character will come, this program will allow it to display it again. hope you can enjoy and understand it.

class Program
    {
        static void Main(string[] args)
        {
            string s = "hemanshu goswami";
            string final = "";
            foreach (char c in s)
            {
                if(string.IsNullOrEmpty(final) || !final.Contains(c)) //check if final string contains any existing character or not.
                {
                    final += c;
                }
            }
            Console.Write(final);
            Console.ReadLine();
 }

output:hemanshu gowi

Remove extra space from string without using inbuilt function.

Below program is for remove extra space from given string.it will remove extra space from given words and give you formatted output.
Hope you will enjoy it.

class Program
    {
        static void Main(string[] args)
        {
            string s = "My name  is    john";
            string final = "";
            int spacecount = 0;
            foreach (char c in s)
            {
                string isEmpty = c.ToString();
                if (isEmpty == " ")
                {
                    spacecount++;
                    continue;
                }
                if (spacecount >= 1)
                {
                    spacecount = 0;
                    final += " ";
                }
                final += c;
             

            }
            Console.Write(final);
            Console.ReadLine();
        }

    }

Output:My Name is john

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...