Highlighting wrong Spelling in web browser control - c#

What I am trying to do is mark a dotted red Underline to each wrong spelling present in the web browser control which i have used in my winform Application.
Here is my Code snippet :-
public static string CheckSpelling(string InnerHTML)
{
string Val = "";
try
{
StringBuilder strBu = new StringBuilder();
strBu.Append(InnerHTML);
RemoveStyleAssigned(ref strBu);
for (int i = 0; i < strBu.Length; i++)
{
if (Convert.ToString(strBu[i]).ToLower() == "<")
{
for (int j = i + 1; j < strBu.Length; j++)
{
if (Convert.ToString(strBu[j]).ToLower() == ">")
{
i = j;
for (int k = j + 1; k < strBu.Length; k++)
{
if (Convert.ToString(strBu[k]).ToLower() != " ")
{
i = k;
CheckAndReplace(ref strBu, ref i);
break;
}
}
break;
}
}
}
else if (Convert.ToString(strBu[i]).ToLower() != " ")
{
CheckAndReplace(ref strBu, ref i);
}
}
Val = strBu.ToString();
}
catch (Exception ex)
{
}
return Val;
}
Here InnerHTML is the InnerHTML of the web browser control obtained for entered data.
The Next Method is CheckandReplace
private static void CheckAndReplace(ref StringBuilder strBu, ref int i)
{
try
{
string Target = string.Empty;
string NewString = "";
for (int j = i; j <= strBu.Length; j++)
{
if (j == strBu.Length || Convert.ToString(strBu[j]).ToLower() == " " || Convert.ToString(strBu[j]).ToLower() == "<" )
{
string Wordtocheck = ReplaceXmlCharacters(Target);
if (!IsSpellingCorrect(Wordtocheck))
{
NewString = "<u style='text-decoration: none; border-bottom: 1px dotted #FF0000'>" + Target + "</u>";
strBu = strBu.Replace(Target, NewString, i, Target.Length);
i += NewString.Length - 1;
}
else
{
i = j - 1;
}
break;
}
else
Target += strBu[j];
}
}
catch (Exception ex)
{
}
}
The Main Problem is that Everything works fine with the above code but whenever i get any special character or any space in the Target Value, the above code also highlight the same but I don't want to highlight that as done in MS Word. Please Guide me through this or is their any other way out.
Thanks in advance

You may want to check out Regex. Seems like it would help with what you are trying to accomplish.

Related

Determine whether each character in the first string can be uniquely replaced by a character in the second string so that the two strings are equal

Give two strings of equal size. Determine whether each character in the first string can be uniquely replaced by a character in the second string so that the two strings are equal. Display also the corresponding character pairs between the two strings. The code works well now.
Example 1:
For input data:
aab
ttd
The console will display:
True
a => t
b => d
Example 2:
For input data:
tab
ttd
The console will display:
False
In the second example the answer is false because there is no unique correspondence for the character 'a': both 't' and 'd' correspond to it.
This is my code:
using System;
namespace problemeJM
{
class Program
{
static void Main(string[] args)
{
string firstPhrase = Convert.ToString(Console.ReadLine());
string secondPhrase = Convert.ToString(Console.ReadLine());
string aux1 = string.Empty, aux2 = string.Empty;
bool x = true;
for (int i = 0; i < firstPhrase.Length; i++)
{
if (!aux1.Contains(firstPhrase[i]))
{
aux1 += firstPhrase[i];
}
}
for (int i = 0; i < secondPhrase.Length; i++)
{
if (!aux2.Contains(secondPhrase[i]))
{
aux2 += secondPhrase[i];
}
}
if (aux1.Length != aux2.Length)
{
Console.WriteLine("False");
}
else
{
for (int i = 0; i < firstPhrase.Length - 2; i++)
{
for (int j = 1; j < secondPhrase.Length - 1; j++)
{
if (firstPhrase[i] == firstPhrase[j] && secondPhrase[i] == secondPhrase[j])
{
x = true;
}
else if (firstPhrase[i] != firstPhrase[j] && secondPhrase[i] != secondPhrase[j])
{
x = true;
}
else if (firstPhrase[i] == firstPhrase[j] && secondPhrase[i] != secondPhrase[j])
{
x = false;
break;
}
else if (firstPhrase[i] != firstPhrase[j] && secondPhrase[i] == secondPhrase[j])
{
x = false;
break;
}
}
}
Console.WriteLine(x);
aux1 = string.Empty;
aux2 = string.Empty;
if (x == true)
{
for (int i = 0; i < firstPhrase.Length; i++)
{
if (!aux1.Contains(firstPhrase[i]))
{
aux1 += firstPhrase[i];
}
}
for (int i = 0; i < secondPhrase.Length; i++)
{
if (!aux2.Contains(secondPhrase[i]))
{
aux2 += secondPhrase[i];
}
}
for (int i = 0; i <= aux1.Length - 1; i++)
{
for (int j = 1; j <= aux2.Length; j++)
{
if (aux1[i] == aux1[j] && aux2[i] == aux2[j])
{
Console.WriteLine(aux1[i] + " => " + aux2[i]);
break;
}
else if (aux1[i] != aux1[j] && aux2[i] != aux2[j])
{
Console.WriteLine(aux1[i] + " => " + aux2[i]);
break;
}
}
}
}
}
}
}
}
I think you should use a Dictionary<char, char> as commented. But you need to check if there's a unique mapping in both string, so from s1 to s2 and from s2 to s1:
static bool UniqueMapping(string s1, string s2)
{
int length = Math.Min(s1.Length, s2.Length);
var dict = new Dictionary<char, char>(length);
for (int i = 0; i < length; i++)
{
char c1 = s1[i];
char c2 = s2[i];
bool contained = dict.TryGetValue(c1, out char c);
if (contained && c2 != c)
{
return false;
}
dict[c1] = c2;
}
return true;
}
Here are your samples. Note that i use UniqueMapping twice(if true after 1st):
static void Main(string[] args)
{
var items = new List<string[]> { new[]{ "aab", "ttd" }, new[] { "tab", "ttd" }, new[] { "ala bala portocala", "cuc dcuc efghficuc" }, new[] { "ala bala portocala", "cuc dcuc efghijcuc" } };
foreach (string[] item in items)
{
bool result = UniqueMapping(item[0], item[1]);
if(result) result = UniqueMapping(item[1], item[0]);
Console.WriteLine($"Word 1 <{item[0]}> Word 2 <{item[1]}> UniqueMapping? {result}");
}
}
.NET Fiddle: https://dotnetfiddle.net/4DtIyH

Unable to move the hash sign the right side

I'm unable to move the hash sign to the right to get the below shape.
My below code is working not as expected but I need to get the below shape.
Please how do I do it?
#
##
###
####
#####
######
#######
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++) {
Console.WriteLine(MySpace(i) + HashSign(i));
}
}
public static string HashSign(int n)
{
string t = "";
for (int i = 1; i <= n; i++) {
t += "#";
}
return t;
}
public static string MySpace(int n)
{
string t = "/t";
for (int i = 1; i < n; i++)
{
t += " ";
}
return t;
}
}
Try this:
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++)
{
Console.WriteLine(" ".PadLeft(n - i+1, ' ')+"#".PadLeft(i,'#'));
}
}
Or make few changes to your code:
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++)
{
Console.WriteLine(MySpace(n - i + 1) + HashSign(i));
}
}
public static string HashSign(int n)
{
string t = "";
for (int i = 1; i <= n; i++)
{
t += "#";
}
return t;
}
public static string MySpace(int n)
{
string t = string.Empty;
for (int i = 1; i < n; i++)
{
t += " ";
}
return t;
}
}
A more memory efficient way would be using the StringBuilder class.
For this situation it's not critical, but nice to know.
// define the amount of steps
int n=8;
// amount of leading whitespaces, for later usage
int padding=0;
// this one is the "working" memory, initialized by n + padding whitespaces
StringBuilder currentLine=new StringBuilder(new string(' ',n+padding));
// it counts down from the last index to the one indicated by padding
for (int i = currentLine.Length-1; i >=padding; i--)
{
// replace the char at the current index with #; (here: always the index of the last whitespace)
currentLine[i]='#';
// display a copy of the current state on the console,
Console.WriteLine(currentLine.ToString());
}
Please change only few things in your code :
public class MyProgramTest
{
public static void StaircaseChallenge(int n)
{
for (int i = 1; i <= n; i++) {
Console.WriteLine(MySpace(i, n) + HashSign(i));
}
}
public static string HashSign(int n)
{
string t = "";
for (int i = 1; i <= n; i++) {
t += "#";
}
return t;
}
public static string MySpace(int m, int n)
{
string t = "";
for (int i = 1; i <= n - m; i++)
{
t += " ";
}
return t;
}
}
You have to pass more one variable is n (number of row) in MySpace() function for leave space. When you pass number of row in MySpace() function then it will leave (number of row - 1) space. So if you enter 5 then first time it will leave 4 space and then put "#" like wise.

How can I find if there is a special character in String ? ex : " ] "

I do readline a long file and want to stop when readline.toString() contain a special character "]"
But my below code not worked as it not recognized and skip the target line.
Please help
do
{
//<My Func>;
k++;
} while (!line[k].ToString().Contains('"' + "]'" + '"'));
I'm not sure what you want to do, but is this what it's like?
public static void Main()
{
var longText = "aadfhhhtgdfg....[]h....";
// check type 1
var pos = longText.IndexOf(']');
if (pos < 0) { }
// check type 2
if (!longText.Contains(']')) { }
// chech type 3
int i = 0;
char c;
do
{
c = longText[i++];
} while (c != ']');
// check type 4
for (var j = 0; j < longText.Length; j++)
{
c = longText[j];
if (c == ']') continue; // skip
// do something
}
Console.WriteLine($"pos = {pos}, i = {i}");
}
It should be this way
do
{
k++;
} while (!line[k].ToString().Contains('"' + "]" + '"'));

Skip text in RichTextBox

I would skip the text in the field, if there is no text, but if this is to add text to the beginning and the end. I'm trying like this but it add text every line.
private void zmien(string a, string b)
{
{
if (richTextBox1.Text.Length > 0)
{
string[] lines = richTextBox1.Lines;
for (int i = 0; i < lines.Length; i++)
{
if (richTextBox1.Text.Length == 0)
{
for (int j = 0; i < lines.Length; i++)
lines[i] = Environment.NewLine;
}
else
lines[i] = a + lines[i] + b;
}
richTextBox1.Lines = lines;
//richTextBox1.SelectedText = "test" + lines;
}
}
}
You can do that much simply as you think, try follow this code
private void zmien(string a, string b)
{
if (richTextBox1.Text.Length > 0)
{
richTextBox1.Text = a+" "+richTextBox1.Text+" "+b;
}
}

How to search and replace words in a WPF TextBox?

I've found several examples that do this but with richTextBox instead. Is it even possible to replace words in a multi-line TextBox?
you can do this for searchin' the next word automatically in textbox
int t = 0;
private void FindNext(object sender, RoutedEventArgs e)
{
for (int i = t; i < NoteText.Text.Length-SearchBar.Text.Length; i++)
{
string x = "";
for (int j = 0; j < SearchBar.Text.Length; j++)
{
if(SearchBar.Text[j] == NoteText.Text[i+j])
{
x += NoteText.Text[i + j] + "";
}
else
{
x = "";
}
}
if(x == SearchBar.Text)
{
t = i+1;
NoteText.Focus();
NoteText.SelectAll();
NoteText.Select(i, SearchBar.Text.Length);
break;
}
if(i==NoteText.Text.Length - SearchBar.Text.Length - 1)
{
MessageBox.Show("The search was completed");
t = 0;
}
s = t;
}
}

Categories