Last active
August 29, 2015 14:22
-
-
Save VegaFromLyra/68b31971851c08a10404 to your computer and use it in GitHub Desktop.
Strings
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
using System; | |
using System.Text; | |
namespace Strings | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("itoa for {0} is {1}", -234, itoa(-234)); | |
Console.WriteLine("itoa for {0} is {1}", 1.1111, itoa(1.1111)); | |
Console.WriteLine("atoi for {0} is {1}", "-234", atoi("-234")); | |
Console.WriteLine("atoi for {0} is {1}", "0", atoi("0")); | |
Console.WriteLine("atod for {0} is {1}", "236.45", atod("236.45")); | |
Console.WriteLine("atod for {0} is {1}", "236", atod("236")); | |
Console.WriteLine("Is {0} a palindrome?: {1}", "abba", isPalindrome("abba")); | |
Console.WriteLine("Is {0} a palindrome?: {1}", "abkeba", isPalindrome("abkeba")); | |
Console.WriteLine("Does {0} have unique characters?: {1}", "abccdef", hasUniqueCharacters("abccdef")); | |
Console.WriteLine("Does {0} have unique characters?: {1}", "×", hasUniqueCharacters("×")); | |
} | |
static string reverse(string s) { | |
StringBuilder sb = new StringBuilder(s); | |
for(int i = 0; i < sb.Length / 2; i++) { | |
char temp = sb[i]; | |
sb[i] = sb[sb.Length - 1 - i]; | |
sb[sb.Length - 1 - i] = temp; | |
} | |
return sb.ToString(); | |
} | |
static string itoa(int num) { | |
bool isNegative = false; | |
if (num == 0) { | |
return "0"; | |
} | |
if (num < 0) { | |
isNegative = true; | |
num *= -1; | |
} | |
StringBuilder sb = new StringBuilder(""); | |
while (num > 0) { | |
char digit = (char)((num % 10) + '0'); | |
sb.Append(digit); | |
num = num / 10; | |
} | |
if (isNegative) { | |
sb.Append('-'); | |
} | |
return reverse(sb.ToString()); | |
} | |
static string itoa(double num) { | |
int integerVal = (int)num; | |
int numOfDecimalPoints = 0; | |
double difference = integerVal * 0.0001; | |
while (Math.Abs((double)integerVal - num) >= difference) { | |
numOfDecimalPoints++; | |
num *= 10; | |
integerVal = (int)(num); | |
Console.WriteLine("Int value is " + integerVal); | |
Console.WriteLine("Double value is " + num); | |
} | |
StringBuilder sb = new StringBuilder(itoa(integerVal)); | |
sb.Insert(sb.Length - numOfDecimalPoints, '.'); | |
return sb.ToString(); | |
} | |
static int atoi(string s) { | |
if (String.IsNullOrEmpty(s)) { | |
throw new Exception("Invalid Input"); | |
} | |
int current = 0; | |
bool isNegative = false; | |
if (s[0] == '-') { | |
current = 1; | |
isNegative = true; | |
} | |
int result = 0; | |
while (current < s.Length) { | |
if (!Char.IsDigit(s[current])) { | |
throw new Exception("Invalid input"); | |
} | |
result = (result * 10) + (int)(s[current] - '0'); | |
current++; | |
} | |
if (isNegative) { | |
result *= -1; | |
} | |
return result; | |
} | |
static double atod(string s) { | |
if (String.IsNullOrEmpty(s)) { | |
throw new Exception("Invalid Input"); | |
} | |
if (s.Contains(".")) { | |
string[] inputs = s.Split('.'); | |
if (inputs.Length > 2) { | |
throw new Exception("Invalid input"); | |
} | |
int beforeDecimal = atoi(inputs[0]); | |
int afterDecimal = atoi(inputs[1]); | |
return (beforeDecimal + (afterDecimal / Math.Pow(10, inputs[1].Length))); | |
} else { | |
return atoi(s); | |
} | |
} | |
static bool isPalindrome(string s) { | |
if (String.IsNullOrEmpty(s) || String.IsNullOrWhiteSpace(s)) { | |
return false; | |
} | |
for (int i = 0; i <= s.Length / 2; i++) { | |
if (s[i] != s[s.Length - 1 - i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
// assuming input only has ASCII characters | |
static bool hasUniqueCharacters(string s) { | |
bool[] charMap = new bool[256]; | |
foreach (char ch in s) { | |
if (charMap[ch]) { | |
return false; | |
} else { | |
charMap[ch] = true; | |
} | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment