Assignment: 1) Write A Program Which Checks If Two Strings Are Anagram or Not?
Assignment: 1) Write A Program Which Checks If Two Strings Are Anagram or Not?
Assignment: 1) Write A Program Which Checks If Two Strings Are Anagram or Not?
AMMU PRIYA
ASSIGNMENT
UNIT -2
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
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
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
using System;
namespace ForgetCode
{
public class MainClass
{
public static void Main()
{
int x = 0, y = 1, z=0, nth, i;
1612009 D.AMMU PRIYA