Check if 2 strings contain same letters at same index [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
string text1 = "abcde";
string text2 = "fgchi";
I want to check if the 2 strings have the same characters at the same index and if they do then let the place where they are the same be printed.

for (int i = 0; i < text1.Length; i++)
if (text1[i] == text2[i])
Console.WriteLine("Character {0} at index {1}", text1[i], i);
Considering your strings have the same length.
Edit: if I should not give answers to trivial tasks such as this and instead encourage the user to find it by himself, then please point out that to me. I'm new around. [I suppose it's obvious, so i'm just not gonna do it, and adjust]

Maybe something like the following code helps. And it shouldn´t be important how long each string is with that. Maybe the string.Format isn´t needed.
private string charMatch(string str_a, string str_b)
{
int char_a = str_a.Count();
int char_b = str_b.Count();
int runs = 0;
StringBuilder sb = new StringBuilder();
if (char_a <= char_b) { runs = char_a; }
else { runs = char_b; }
for (int i = 0; i < runs; i++)
{
if (str_a[i] == str_b[i])
{
sb.Append(string.Format("Match found at {0} \n", i));
}
}
return sb.ToString();
}

Related

indexes of multiple occurences in string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I need help with this part of my code.
I'm checking the occurences of char within a string:
public string Obsahuje(string slovo, char pismeno)
{
if (slovo.Contains(pismeno))
{
char[] array = uhodnute.ToCharArray();
int index;
int zacatek = 0;
do
{
index = slovo.IndexOf(pismeno, zacatek);
array[index] = pismeno;
zacatek = index;
} while (slovo.IndexOf(pismeno, zacatek + 1) != -1);
uhodnute = new string(array);
return uhodnute;
}
--zivot;
return uhodnute;
}
It works fine with character that occurs just once, but when I try another that occurs twice or more times, the program just freezes. No error, it just stops responding. I know it's probably something very stupid, but I'm doing this for so long that I just can't see it. I'd appreciate some help.
Edit: It works with:
do
{
index = slovo.IndexOf(pismeno, index);
array[index] = pismeno;
index++;
} while (slovo.IndexOf(pismeno, index) != -1);
The problem is that you don't update the value of index, and the code freezes because it remains in the while loop for ever.
int index;
int zacatek = 0;
do
{
index = slovo.IndexOf(pismeno, zacatek);
array[index] = pismeno;
index++;
zacatek = index;
} while (slovo.IndexOf(pismeno, zacatek + 1) != -1);

compare different combinations of a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a dictionary with key value pairs something like below
dict("A-B-C", "This is abc");
dict("X-Y-Z", "This is xyz");
so on
now when I receive an input say something like "B-A-C" the system should intelligent to know its "A-B-C" or if its "Z-Y-X" should map to "X-Y-Z", one way is to put all the combinations into the dictionary such that say dict("B-A-C", "A-B-C") but this may lead to more maintainability thing, just thinking if there is anything from .NET framework can address this issue more easily.
Is there an easy way to compare a string like below
A string either A-B or B-A is equal to A-B
A string either A-B-C or B-C-A or A-C-B or C-A-B or C-B-A equals to A-B-C
likewise a different combinations of A-B-C-D should equals to A-B-C-D
so as A-B-C-D-E
so on
A B C D E in the above example are a two letter non numeric word. something like
Su-Ma-Ju-Ve
UPDATE
For now I solved this with below code which returns the string tokens in the order which is recognized in the dictionary I maintain, not sure if its the best way but is solving the problem for now
string sortPls(string pls)
{
Dictionary<string, int> dctPls = new Dictionary<string, int>();
dctPls["Su"] = 1;
dctPls["Mo"] = 2;
dctPls["Ju"] = 3;
dctPls["Me"] = 4;
dctPls["Ve"] = 5;
dctPls["Ma"] = 6;
dctPls["Sa"] = 7;
string[] arrPls = pls.Split('-');
int j = 0;
string sortPls = string.Empty;
for(int i = 0; i < arrPls.Length; i++)
{
for (j = i + 1; j < arrPls.Length; j++)
{
if (dctPls[arrPls[j]] < dctPls[arrPls[i]])
{
string tmp = arrPls[i];
arrPls[i] = arrPls[j];
arrPls[j] = tmp;
}
}
}
for (int k = 0; k < arrPls.Length; k++)
sortPls += arrPls[k] + "-";
return sortPls.Remove(sortPls.Length - 1) ;
}
Yes. If looking for set equality, then HashSet<T> gives you the tools.
"ABBA".ToHashSet().SetEquals("BA")
Otherwise for an "anagram" type comparison, order the characters and compare sequences:
"CDAB".OrderBy(x=>x).SequenceEqual("DCBA".OrderBy(x=>x))
Split the string into individual letters, add the letters to a Set, and compare the sets using their Equals methods
void Main()
{
string check = "CADB";
string equalTo = new string(check.ToCharArray().OrderBy(x => x).ToArray());
Console.WriteLine(equalTo);
}
is just one way to do that.
I think, this will be the fastest way to do it:
bool StringEquals(string string1, string string2)
{
foreach (char ch in string1)
{
if (!string2.Contains(ch))
{
return false;
}
}
return true;
}
Sort the characters in each string, any two strings that have the same characters will have their sorted version the same. Stick the results into a `Dictionary>, with the sorted version as the key.

String vector with user input [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Stuck for hours on this task. I really appreciate help, it is not shown in the literature how this is to be done. I can create the vector but after that I'm totally blocked.
Create a string vector with five elements. The user will then enter 5 names via a for loop. The program then writes these names via another for loop.
Please type out the code so I can understand. Without solving this first task I can't do the other ones.
Here's my futile attempt so far
int [] namn = new int [5];
for (int i = 0; i < 5; i++);
{
Console.Write("Ange fem namn");
string str = Console.ReadLine();
int names = Convert.ToInt32(str);
}
It seems that the main (and the most difficult error to find) is ; after for loop
for (int i = 0; i < 5; i++);
this loop does nothing five times.
//DONE: "string[]" - Create a STRING vector with five elements...
string [] namn = new string [5];
//DONE: namn.Length - no magic numbers (5)
for (int i = 0; i < namn.Length; i++) // !!! no ";" !!!
{
Console.Write("Ange fem namn");
//DONE: put into array, not to a local variable
namn[i] = Console.ReadLine();
}
// Print out the names
foreach(var item in namn)
Console.WriteLine(item);

MinHeap implementation in c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am working on a MinHeap implementation for school and I have encountered a problem. The code typically works well but sometimes generates an argument out of range exception in my heapify method. I have tried to isolate the problem but I am a terrible debugger.
Here is my code for the function:
private void Heapify(int i)
{
int least;
int leftchild = 2 * (i + 1) - 1;
int rightchild = 2 * (i + 1);
if (leftchild < heap.Count && (heap[rightchild].CompareTo(heap[i]) < 0))
{
least = 1;
}
else
{
least = i;
}
if (rightchild < heap.Count && (heap[rightchild].CompareTo(heap[least]) < 0))
{
least = rightchild;
}
if (least != i)
{
T temp = heap[i];
heap[i] = heap[least];
heap[least] = temp;
this.Heapify(least);
}
Out of range exceptions are generally easy to track down. Basically, you have to make sure that whenever you're accessing an item in an array via indexes, said array's count / length is greater than the index. In other words, ensure that in every call to heap[#index], #index < heap.Count (either by straight checking it or by your method's logic)
if (leftchild < heap.Count && (heap[rightchild].CompareTo(heap[i]) < 0))
If rightchild >= heap.Count, this will give you an exception.

How do I build a function in C# that returns a delimited set of string numbers? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone give me an example of how to build a function in C# that returns a delimited set of string numbers?
If anyone could help that would be amazing. Thanks.
EDIT: Now answered. Thanks T.S. for the help.
Along the lines of what I was going for with:
public string GetDelimited(int noOfLoops, string delimiter)
{
var build = new StringBuilder();
for (int j=1; j< noOfLoops + 1; j++)
{
if (j > 1) builder.Append(delimiter)
}
return build.ToString();
}
This is example in the purest, basic, sugar-less form
public string GetDelimited(int numberOfLoops, string delimiter)
{
var builder = new StringBuilder();
for (int i= 1; i < numberOfLoops + 1; i++)
{
if (i > 1) builder.Append(delimiter);
builder.Append(i.ToString());
}
return builder.ToString();
}

Categories