0% found this document useful (0 votes)
34 views8 pages

Assignment: 1) Write A Program Which Checks If Two Strings Are Anagram or Not?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

1612009 D.

AMMU PRIYA

C # AND .NET TECHNOLOGIES

ASSIGNMENT

UNIT -2

1)Write a Program which checks if two Strings are Anagram or not?

using System; 
                      
public class GFG { 
  
    static int NO_OF_CHARS = 256; 
  
    static bool areAnagram(char []str1, char []str2) 
    { 
        int []count1 = new int[NO_OF_CHARS]; 
        int []count2 = new int[NO_OF_CHARS]; 
        int i; 
  
        for (i = 0; i < str1.Length && i < str2.Length; 
             i++) { 
            count1[str1[i]]++; 
            count2[str2[i]]++; 
        } 
  
        if (str1.Length != str2.Length) 
            return false; 
  
     
        for (i = 0; i < NO_OF_CHARS; i++) 
            if (count1[i] != count2[i]) 
                return false; 
  
        return true; 
    } 
  
1612009 D.AMMU PRIYA

    
    public static void Main() 
    { 
        char []str1 = ("silent").ToCharArray(); 
        char []str2 = ("listen").ToCharArray(); 
  
        if (areAnagram(str1, str2)) 
            Console.WriteLine("The two strings are anagram of each other"); 
        else
            Console.WriteLine("The two strings are not anagram of each
other"); 
    } 
} using System; 

                      
public class GFG { 
  
    static int NO_OF_CHARS = 256; 
  
    static bool areAnagram(char []str1, char []str2) 
    { 
        int []count1 = new int[NO_OF_CHARS]; 
        int []count2 = new int[NO_OF_CHARS]; 
        int i; 
  
        for (i = 0; i < str1.Length && i < str2.Length; 
             i++) { 
            count1[str1[i]]++; 
            count2[str2[i]]++; 
        } 
  
        if (str1.Length != str2.Length) 
            return false; 
  
     
        for (i = 0; i < NO_OF_CHARS; i++) 
            if (count1[i] != count2[i]) 
                return false; 
  
        return true; 
1612009 D.AMMU PRIYA

    } 
  
    
    public static void Main() 
    { 
        Console.WriteLine("Enter string 1");
        char []str1=Console.ReadLine().ToCharArray();
        Console.WriteLine("Enter string 2");
        char []str2=Console.ReadLine().ToCharArray(); 
    
  
        if (areAnagram(str1, str2)) 
            Console.WriteLine("The two strings are anagram of each other"); 
        else
            Console.WriteLine("The two strings are not anagram of each
other"); 
    } 

1612009 D.AMMU PRIYA

2)Write a function to remove duplicate characters from String? 

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

namespace Demo {
  public class Program {
     public static void Main(string[] args) {
         string myStr = "kkllmmnnoo";
         Console.WriteLine("Initial String: "+myStr);
         var unique = new HashSet<char>(myStr);
         Console.Write("New String after removing duplicates: ");
         foreach (char c in unique) 
            Console.Write(c);   
   }
   }
}
1612009 D.AMMU PRIYA

3) How to swap two numbers without using a temp variable, write code
which is free from Integer overflow?
Program:

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

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter x");
int x = int.Parse(Console.ReadLine());
Console.WriteLine("Enter y");
int y = int.Parse(Console.ReadLine());
Console.WriteLine("Before swap:");
Console.WriteLine("x value: " + x);
Console.WriteLine("y value: " + y);
x = x + y;
y = x - y;
x = x - y;
Console.WriteLine("After swap:");
Console.WriteLine("x value: " + x);
Console.WriteLine("y value: " + y);
Console.ReadKey();
}
}
}

Output:
1612009 D.AMMU PRIYA

4)How to find all pairs of elements in an integer array, whose sum is


equal to a given number?

using System; 
using System.Collections.Generic; 
public class GFG 

public static void printpairs(int []arr, int sum) 

HashSet<int> s = new HashSet<int>(); 
for (int i = 0; i < arr.Length; ++i) 

int temp = sum - arr[i];  
if (temp >= 0 && s.Contains(temp)) 

Console.Write("\nPair with given sum " + 
sum + " is (" + arr[i] + 
", " + temp + ")"); 

s.Add(arr[i]); 


public static void Main () 

1612009 D.AMMU PRIYA

Console.WriteLine("Enter the no.of elements");


int n=int.Parse(Console.ReadLine());
int []A = new int[n];
int i;
for(i=0;i<n;i++)
{
Console.WriteLine("Enter the elements {0}",i);
A[i]=int.Parse(Console.ReadLine());
}
printpairs(A, n); 

5) Write a function to print nth number in Fibonacci series?

using System;
namespace ForgetCode
{
    public class MainClass
  {
        public static void Main()
    {
            int x = 0, y = 1, z=0, nth, i;
1612009 D.AMMU PRIYA

            Console.WriteLine("\n\nPlease Enter The Term Number:");


            nth = Convert.ToInt32(Console.ReadLine());
            for (i = 1; i <= nth; i++)
      {
                z = x + y;
                x = y;
                y = z; 
      }
 
            Console.WriteLine("\nthe {0}th term of Fibonacci Series is is
{1}\n\n\n", nth, z);           
 
    }

You might also like