Cerebrus

String Interleaving

Oct 21st, 2009
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. //Related to Question at:
  2. //http://groups.google.com/group/dotnetdevelopment/browse_thread/thread/94c1efd1c6a26e3c
  3.  
  4. class Program
  5. {
  6.   static void Main(string[] args)
  7.   {
  8.     // The Parse method parses a given string. This was only for testing. I then used the same logic for the ParseFile() method.
  9.     //List<string> strings = Parse(args[0]);
  10.  
  11.     List<string> listA = ParseFile(@"C:\strings.txt");
  12.     List<string> listB = ParseFile(@"C:\strings2.txt");
  13.  
  14.     List<string> listC = Interpolate(listA, listB).ToList();
  15.  
  16.     //TODO: Output listC to a third file. No big deal here.
  17.   }
  18.  
  19.   private static IEnumerable<string> Interpolate(List<string> listA, List<string> listB)
  20.   {
  21.     //List<string> list = listA.Count < listB.Count ? listA : listB;
  22.     // An iterator block is overkill, here. Used for convenience only.
  23.     for (int i = 0; i < listA.Count; i++)
  24.     {
  25.       yield return listA[i];
  26.       yield return listB[i];
  27.     }
  28.   }
  29.  
  30.   private static List<string> Parse(string p)
  31.   {
  32.     List<string> list = new List<string>();
  33.     int stringLen = p.Length;
  34.     if (stringLen > 0)
  35.     {
  36.       char c = p[0];
  37.       StringBuilder sb = new StringBuilder(c.ToString());
  38.       for (int i = 1; i < stringLen; i++)
  39.       {
  40.         char nextC = p[i];
  41.         if (char.Equals(c, nextC))
  42.         {
  43.           sb.Append(nextC);
  44.           continue;
  45.         }
  46.         list.Add(sb.ToString());
  47.         sb = new StringBuilder(nextC.ToString());
  48.         c = nextC;
  49.       }
  50.  
  51.       list.Add(sb.ToString());
  52.     }
  53.     return list;
  54.   }
  55.  
  56.   private static List<string> ParseFile(string fName)
  57.   {
  58.     using (FileStream fs = new FileStream(fName, FileMode.Open, FileAccess.Read, FileShare.None))
  59.     {
  60.       using (StreamReader sr = new StreamReader(fs))
  61.       {
  62.         List<string> list = new List<string>();
  63.         int i = sr.Read();
  64.         if (i > -1)
  65.         {
  66.           char c = (char)i;
  67.           StringBuilder sb = new StringBuilder(c.ToString());
  68.           while ((i = sr.Read()) > 0)
  69.           {
  70.             char nextC = (char)i;
  71.             if (char.Equals(c, nextC))
  72.             {
  73.               sb.Append(nextC);
  74.               continue;
  75.             }
  76.  
  77.             list.Add(sb.ToString());
  78.             sb = new StringBuilder(nextC.ToString());
  79.             c = nextC;
  80.           }
  81.  
  82.           list.Add(sb.ToString());
  83.         }
  84.         return list;
  85.       }
  86.     }
  87.   }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment