Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Related to Question at:
- //http://groups.google.com/group/dotnetdevelopment/browse_thread/thread/94c1efd1c6a26e3c
- class Program
- {
- static void Main(string[] args)
- {
- // The Parse method parses a given string. This was only for testing. I then used the same logic for the ParseFile() method.
- //List<string> strings = Parse(args[0]);
- List<string> listA = ParseFile(@"C:\strings.txt");
- List<string> listB = ParseFile(@"C:\strings2.txt");
- List<string> listC = Interpolate(listA, listB).ToList();
- //TODO: Output listC to a third file. No big deal here.
- }
- private static IEnumerable<string> Interpolate(List<string> listA, List<string> listB)
- {
- //List<string> list = listA.Count < listB.Count ? listA : listB;
- // An iterator block is overkill, here. Used for convenience only.
- for (int i = 0; i < listA.Count; i++)
- {
- yield return listA[i];
- yield return listB[i];
- }
- }
- private static List<string> Parse(string p)
- {
- List<string> list = new List<string>();
- int stringLen = p.Length;
- if (stringLen > 0)
- {
- char c = p[0];
- StringBuilder sb = new StringBuilder(c.ToString());
- for (int i = 1; i < stringLen; i++)
- {
- char nextC = p[i];
- if (char.Equals(c, nextC))
- {
- sb.Append(nextC);
- continue;
- }
- list.Add(sb.ToString());
- sb = new StringBuilder(nextC.ToString());
- c = nextC;
- }
- list.Add(sb.ToString());
- }
- return list;
- }
- private static List<string> ParseFile(string fName)
- {
- using (FileStream fs = new FileStream(fName, FileMode.Open, FileAccess.Read, FileShare.None))
- {
- using (StreamReader sr = new StreamReader(fs))
- {
- List<string> list = new List<string>();
- int i = sr.Read();
- if (i > -1)
- {
- char c = (char)i;
- StringBuilder sb = new StringBuilder(c.ToString());
- while ((i = sr.Read()) > 0)
- {
- char nextC = (char)i;
- if (char.Equals(c, nextC))
- {
- sb.Append(nextC);
- continue;
- }
- list.Add(sb.ToString());
- sb = new StringBuilder(nextC.ToString());
- c = nextC;
- }
- list.Add(sb.ToString());
- }
- return list;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment