Created
December 28, 2016 05:32
-
-
Save PradeepLoganathan/c923c17958bf2a62c41635454d13e084 to your computer and use it in GitHub Desktop.
Strings and Arrays in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Hashtable ht = new Hashtable(); | |
ht.Add("f", 1); | |
ht.Add("S", 2); | |
ht.Add("j", 3); | |
ht.Add("ffg", 4); | |
ht.Add("yh",5); | |
ht.Add("rr", 6); | |
StringBuilder sb = new StringBuilder(); | |
sb.Append("Hello "); | |
sb.Append(", "); | |
sb.Append("This "); | |
sb.Append("is "); | |
sb.Append("Me. "); | |
Console.WriteLine("value at key S is {0}", ht["ffg"].ToString()); | |
Console.WriteLine(sb); | |
CheckUniqueString("abcdefghi"); | |
Urlifyastring("THis is a string that has to be urilfyied", 1); | |
CheckPermutation("Hello", "oelHl"); | |
} | |
static void CheckUniqueString(string str) | |
{ | |
bool IsUnique = true; | |
for(int i = 0; i <= str.Length; i++) | |
{ | |
for (int j = i + 1; j < str.Length; j++) | |
{ | |
if (str[i] == str[j]) | |
{ | |
IsUnique = false; | |
break; | |
} | |
} | |
} | |
if(IsUnique) | |
Console.WriteLine("String is unique"); | |
else | |
Console.WriteLine("String is not unique"); | |
} | |
static void CheckPermutation(string str1, string str2) | |
{ | |
char[] c1 = str1.ToCharArray(); | |
char[] c2 = str2.ToCharArray(); | |
Array.Sort(c1); | |
Array.Sort(c2); | |
if(c1.SequenceEqual(c2)) | |
Console.WriteLine("{0} is a permutation of {1} ", str1, str2); | |
else | |
Console.WriteLine("{0} is not a permutation of {1} ", str1, str2); | |
} | |
//URLify: Write a method to replace all spaces in a string with '%20'. You may assume that the string | |
//has sufficient space at the end to hold the additional characters, and that you are given the "true" | |
//length of the string. | |
static void Urlifyastring(string str, int algotype) | |
{ | |
if (algotype == 1)// additional space and not inplace | |
{ | |
StringBuilder URLString = new StringBuilder(); | |
for (int i = 0; i < str.Length; i++) | |
{ | |
if (str[i] != ' ') | |
{ | |
URLString.Append(str[i]); | |
} | |
else | |
{ | |
URLString.Append("%20"); | |
while (str[i] == ' ') | |
{ | |
++i; | |
} | |
i--; | |
} | |
} | |
Console.WriteLine("THe URLified string is {0}", URLString); | |
} | |
else if (algotype == 2) //Inplace | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment