TextFile1.txt:
START FILE 000INDEX 001PRTBNR Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
MaskFile1.txt:
START MASK 000INDEX 001PRTBNR Lorum ipsam dolor sit amut, consectetur adipiscing elit, sed da eiusmod tempor incididunt ut labore et dolore magna aliqu.
expected output:
START #### 000INDEX 001PRTBNR Lor#m ips#m dolor sit am#t, consectetur adipiscing elit, sed d# eiusmod tempor incididunt ut labore et dolore magna aliqu#.
static void Main(string[] args)
{
string path = @"TextFile1.txt";
string mask = @"MaskFile1.txt";
StringBuilder ma = new StringBuilder();
StringBuilder pa = new StringBuilder();
char MaskStamp = '#';
Console.WriteLine();
MaskHandler(MaskFile(mask));
PathHandler(ReadFile(path));
object m = ma.Append(MaskFile(mask).ToString());
object p = pa.Append(MaskFile(path).ToString());
path = pa.ToString();
mask = ma.ToString();
//TestingEquality(path,mask);
foreach (char c in path)
{
foreach (char x in mask)
{
for(int i = 1; i < path.Length; i )
{
if (path[c] != mask[x])
{
{
pa.Insert(i, MaskStamp);
Console.WriteLine(i);
}
}
else {
}
}
}
}
My current output is an infinite loop of the value of "i" but before, I had the line where the index is replaced with a different value but was getting the opposite result, meaning it was only placing "#" at the chars that were in both no the differences. Where is the loop not being told to stop, why isn't "else" handling it? Why is it not stopping at the end of the two text files? I basically have destroyed the project trying to figure out how this won't stop, and I am positive that it's not from the methods being used in the Main.
Any solutions that will get me closer to the desired output are appreciated.
CodePudding user response:
The assignment seems to be this:
Given two (equal length) strings, compare them character-by-character, and print the character if they are equal, or "#" if they are different in an output string.
This can be massively simplified by using Linq's Zip
method.
using System;
using System.Linq;
var input1 = "abc def 123";
var input2 = "acc dez 125";
var outputLetters = input1.Zip(input2, (one, two) => {
// for each pair of characters (one and two), check if they are the same
if (one == two)
return one; // same, print the input character
else
return '#'; // not the same, print #
})
.ToArray();
var output = new string(outputLetters);
Console.WriteLine(output);
This example prints a#c de# 12#
.
The one
and two
variables give you the pairs of characters at each matching index along both strings. For example, the first (0) index of each string would give you a
for both one
and two
, but for the second (1) index one
would be b
and two
would be c
.
CodePudding user response:
A LINQ version:
var t = File.ReadAllText("TextFile1.txt");
var m = File.ReadAllText("MaskFile1.txt");
var o = string.Concat(t.Select((c,i) => c == m[i] ? c : '#'));
A non LINQ version:
var t = File.ReadAllText("TextFile1.txt");
var m = File.ReadAllText("MaskFile1.txt");
var sb = new StringBuilder();
for(int i = 0; i < t.Length; i )
sb.Append(t[i] == m[i] ? c : '#');
As to what is going wrong for you; this is problematic:
foreach (char c in path)
{
foreach (char x in mask)
{
for(int i = 1; i < path.Length; i )
If you are stepping through two strings looking at character positions you don't need 3 loops to do it. If you think about the logic of this for a moment, for every char in A , for every char in B, for nearly every char in A again, if A and B have 10 chars, this innermost loop will run nearly a thousand times...
If you want to loop and compare the chars in two strings you cannot do it with foreach, especially not nested foreach; every time you nest you multiply the number of operations you carry out by the length of the array. Instead you just run one loop with an integer that goes from 0 to the length of the string. Strings can be accessed as if they were char arrays; myString[4]
gets the fifth character of a myString
. You step through each string once comparing the chars in the same positions