Friday, September 27, 2013

String Compare without using String Function

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CompareString
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0, len1 = 0,len2=0;
            Console.WriteLine("\nEnter the String 1");
            string str1 = Console.ReadLine();
            Console.WriteLine("\nEnter the String 2");
            string str2 = Console.ReadLine();          

            foreach (char obj in str1)
            {
                len1++;
            }
            Console.WriteLine("\nString 1 Length:\t"+len1);

            foreach (char obj in str2)
            {
                len2++;
            }
            Console.WriteLine("\nString 2 Length:\t"+len2);

            if (len1==len2)
            {
                for (int i = 0; i < len1; i++)
                {
                    if (str1[i] == str2[i])
                    {
                        count++;
                    }
                    else
                    {
                        count=0;
                    }
                }
            }
            if (count==len1)
            {
                Console.WriteLine("\nStrings are Equal");
            }
            else
            {
                Console.WriteLine("\nStrings are not Equal");
            }

            Console.ReadLine();
        }
    }
}

Output

Strings Compare without using String Functions

No comments:

Post a Comment