Skip to content

Commit

Permalink
Create strcmp.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-sm committed Jun 4, 2013
1 parent b59d5fd commit 193b117
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions strcmp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// strcmp
//Rextester.Program.Main is the entry point for your code. Don't change it.

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

namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine(string.Compare("Helz", "Hela"));
Console.WriteLine(strcmp("Helz", "Hela"));
Console.WriteLine("---");

Console.WriteLine(string.Compare("", "ABCD"));
Console.WriteLine(strcmp("", "ABCD"));
Console.WriteLine("---");

Console.WriteLine(string.Compare("aa", "a"));
Console.WriteLine(strcmp("aa", "a"));
Console.WriteLine("---");

Console.WriteLine(string.Compare("Hello", "World"));
Console.WriteLine(strcmp("Hello", "World"));
Console.WriteLine("---");

Console.WriteLine(string.Compare("ABCD", "abcdEghe"));
Console.WriteLine(strcmp("ABCD", "Hela"));
Console.WriteLine("---");

Console.WriteLine(string.Compare("GeeksForGeeks", "gEEksFORGeEKs"));
Console.WriteLine(strcmp("GeeksForGeeks", "gEEksFORGeEKs"));
Console.WriteLine("---");
}

static int strcmp(string s1, string s2)
{
int i;

var c1 = s1.ToCharArray();
var c2 = s2.ToCharArray();

for (i = 0; i < c1.Length && i < c2.Length; i++)
{
//Console.WriteLine(c1[i] + "," + c2[i]);

if (char.ToLower(c1[i]) == char.ToLower(c2[i]))
{
continue;
}
else
{
break;
}
}

// Console.WriteLine(i);

var ch1 = (c1.Length == i ? char.MinValue : c1[i]);
var ch2 = (c2.Length == i ? char.MinValue : c2[i]);

// Console.WriteLine(ch1 + "," + ch2);

if (ch1 == ch2)
{
return 0;
}

if (ch1 < ch2)
{
return -1;
}

return 1;
}
}
}

0 comments on commit 193b117

Please sign in to comment.