How to solve this challenge using a FIFO stack? - c#

I'm using a portion of C# code from Sanjit Prasad to solve the challenge of processing backspaces in a given string of words. The new challenge is to process the left-arrow-key and right-arrow-key in combination with backspaces, reflecting a "corrector" for typos in writing.
The following string represents the problem and solution for the first challenge using a FIFO stack (credits to Sanjit Prasad):
string: thiss# is a txt##ext with some typos
expected result: this is a text with some typos
This is the code to generate the expected result:
static String finalAnswer(String S)
{
Stack<Char> q = new Stack<Char>();
for (int i = 0; i < S.Length; ++i)
{
if (S[i] != '#') q.Push(S[i]);
else if (q.Count!=0) q.Pop();
}
String ans = "";
while (q.Count!=0)
{
ans += q.Pop();
}
String answer = "";
for(int j = ans.Length - 1; j >= 0; j--)
{
answer += ans[j];
}
return answer;
}
That code works great, now, the challenge is to process the following string:
string: ths#is is an te\\\#///xt wit some\\\\\h///// tpos###ypos
expected result: this is a text with some typos
In the above string, the character "\" represents a left arrow key pressed, and "/" a right arrow key pressed.
Thank you so much for all your comments, this is my very first question in Stackoverflow, I would like to know an approach to solve the this challenge.

Here is a version that gets the job done, but will need some checks for edge cases and some more error checks.
static string GenerateUpdatedString(string strInput)
{
var stringStack = new Stack<char>();//holds the string as it is read from the input
var workingStack = new Stack<char>();//hold chars when going back to fix typos
char poppedChar;
foreach (var ch in strInput)
{
switch (ch)
{
case '\\':
{
PushAndPopCharacters(workingStack, stringStack);
break;
}
case '/':
{
PushAndPopCharacters(stringStack, workingStack);
break;
}
case '#':
{
stringStack.TryPop(out poppedChar);
break;
}
default:
stringStack.Push(ch);
break;
}
}
return new string(stringStack.Reverse().ToArray());
}
static void PushAndPopCharacters(Stack<char> stackToPush, Stack<char> stackToPop)
{
char poppedChar;
if (stackToPop.TryPop(out poppedChar))
{
stackToPush.Push(poppedChar);
}
}
Usage
var result = GenerateUpdatedString
(#"ths#is is an te\\\#///xt wit some\\\\\h///// tpos###ypos");

Related

C# Counting the number of upper and lower case letters in a string

Hi I've been asked to do a task which calculates the number of vowels, consonants, upper and lower case letters in a string.
I'm having trouble working out upper and lower case letters in a string.
I can successfully count the number of vowels and constants but upper and lower case letters seems to be a pain.
Here's the code:
public void Calculate()
{
foreach(string sentence in sentenceList)
{
sentences++;
for (int i = 0; i < sentence.Length; i++)
{
if (vowelsArray.Contains(sentence[i]))
{
vowels++;
}
else if (consonantsArray.Contains(sentence[i]))
{
consonants++;
}
else if (char.IsUpper(sentence[i]))
{
upperCaseLetters++;
}
else if (char.IsLower(sentence[i]))
{
lowerCaseLetters++;
}
}
}
}
The value for the upper and lower case letters is 0. (It shouldn't be)
Any suggestions? Thanks!
You don't need else before
else if (char.IsUpper(sentence[i]))
Because you have two independent sets of conditions:
Vowel / Consonant
UpperCase / LowerCase
You have a chain of if/else statements and the first condition that is matched (either the vowels or consonants) will prevent any future conditions from being matched. Break the if/else chain into 2 chains:
Vowels vs Consonants
Uppercase vs Lowercase
See updated code below:
public void Calculate()
{
foreach(string sentence in sentenceList)
{
sentences++;
for (int i = 0; i < sentence.Length; i++)
{
if (vowelsArray.Contains(sentence[i]))
{
vowels++;
}
else if (consonantsArray.Contains(sentence[i]))
{
consonants++;
}
// the else was removed here!
if (char.IsUpper(sentence[i]))
{
upperCaseLetters++;
}
else if (char.IsLower(sentence[i]))
{
lowerCaseLetters++;
}
}
}
}
You're only going to hit a single one of those conditions each time through the loop, so if your first two conditions cover every possibility (which is pretty likely, since that covers all vowels and all consonants!) you never reach your third and fourth blocks.
I have tested this using dotnetfiddle
But here is the code that worked for me:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
int upper = 0;
int lower = 0;
string upperLowerCase = "This Is A Test";
char[] splitString = upperLowerCase.ToCharArray();
for(int i = 0; i < splitString.Length; i++)
{
if(char.IsUpper(splitString[i]))
{
upper++;
}
else
{
lower++;
}
}
Console.WriteLine("Total Upper Case Letters: " + upper.ToString());
Console.WriteLine("Total Lower Case Letters: " +lower.ToString());
}
}
// Output
// Total Upper Case Letters: 4
// Total Lower Case Letters: 10
But in your case you need to separate conditional statements. One to check for vowels or consonants and the other to check the case of the letter.
So this:
else if (char.IsUpper(sentence[i]))
{
upperCaseLetters++;
}
else if (char.IsLower(sentence[i]))
{
lowerCaseLetters++;
}
Needs to be changed to:
if (char.IsUpper(sentence[i]))
{
upperCaseLetters++;
}
else
{
lowerCaseLetters++;
}
I hope this helps!
string text = "This is Sample";
int upcount = 0;
int lowcount = 0;
for (int i = 0; i < text.Length; i++)
{
if (char.IsUpper(text[i])) upcount++;
if (char.IsLower(text[i])) lowcount++;
}
Console.Write(upcount);
Console.Write(lowcount);
EDIT
In your case change it like this,
if (char.IsUpper(sentence[i]))
{
upperCaseLetters++;
}
else (char.IsLower(sentence[i]))
{
lowerCaseLetters++;
}
Slightly faster version based on Sajeetharan's answer
string text = "This is Sample";
int upcount = 0;
int lowcount = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i]>64 && text[i]<91) upcount++;
else if (text[i]>96 && text[i]<123) lowcount++;
}
Console.Write(upcount);
Console.Write(lowcount);

C# how can I check users input

I´m having a string with allowed chars. I´d like that user is only able to use this chars.
My idea was to loop through the unser inputs string and compare char for char. But the problem which I have is when the first char in string allowed is "A" and the first in the users input "B" is, it gives me an error...
Totally confused right now
string allowed = "abc";
string read= Console.ReadLine();
for (int i = 0; i < allowed.Length; i++ )
{
if (allowed[i] == read[i])
{
Console.WriteLine("Okay");
}
else
{
Console.WriteLine("Invalid char on" +index);
}
}
If you wanna check if the user input has any of not allowed characters you need a nested loop, because you wanna compare each char in the user input against the chars in the allowed:
foreach(var r in read)
{
bool isValid = false;
foreach(var c in allowed)
{
// if we found a valid char set isValid to true
if(c == r)
isValid = true;
}
// if it's still false then the current char
// doesn't match any of the allowed chars
// so it's invalid
if(!isValid)
{
Console.WriteLine("the string has invalid char(s)");
break;
}
}
Or, to simplify this you can use LINQ:
bool isInvalid = read.Any(c => !allowed.Contains(c));
If you want to know which chars are invalid, you can use Except method:
var invalidChars = read.Except(allowed);
foreach(var c in invalidChars)
{
Console.WriteLine(c);
}
You either need to search the char of user input within the allowed characters or you could use a regular expression.
Search approach:
private string allowed = "abc";
private string userInput = "some string entered";
bool stringIsValid = false;
for (int i = 0; i < userInput.Length; i++)
{
if (!allowed.IndexOf(userInput[i]))
{
stringIsValid = false;
break; // You can stop the loop upon the first occurance of an invalid char
}
}
Regular expression approach:
private string allowed = "abc";
private string userInput = "some string entered";
bool stringIsValid = Regex.IsMatch(allowed, userInput);
Please note that the regular expression approach is more flexible. If you learn about regular expressions, you will find it very powerful.
You need another loop in your first one:
string allowed = "abc";
string read= Console.ReadLine();
for (int i = 0; i < read.Length; i++ )
{
bool isValid = false;
for (int j = 0; j < allowed.Length; j++)
{
if (read[i] == allowed[j])
{
isValid = true;
break;
}
}
if (isValid)
{
Console.WriteLine("Okay");
}else{
Console.WriteLine("Invalid char on" +index);
}
}
Right now, what you're saying is "every character in read must be exactly the same as in allowed".
What you're trying to say (I think) is "every character in read must be present somewhere in allowed" – that's what the second loop does. It looks for the character in allowed and if it finds it, sets isValid to true. Otherwise the character wasn't found and it's incorrect.
As other answers here state, you can use LINQ or (preferrably) regular expressions (regex) for things like this. I assume this is homework, or you're new to C# or programming, so I provided a basic answer to (hopefully) help you understand what's not working currently with your code.
If this should indeed be a homerwok or studying-related question, then let me recommend you put that in your question next time, for it's not forbidden to ask about homework.
The "real world" solutions we would use are of no help to you if you're trying to figure out the basics, so if we know it's about learning stuff then we'll provide answers that are more useful for you.
When using a collection to store not allowed items (instead of a plain string) it opens a whole new spectrum of LINQ expressions you can use:
public static void Main(string[] args)
{
var allowed = new List<string> { "a", "b", "c" };
var read = Console.ReadLine().Select(c => c.ToString()).ToList();
if (read.All(allowed.Contains))
{
Console.WriteLine("Okay");
}
else
{
var firstNotAllowed = read.First(a => !allowed.Contains(a));
var firstIndex = read.FindIndex(a => !allowed.Contains(a));
Console.WriteLine("Invalid char: {0}, at index: {1}", firstNotAllowed, firstIndex);
}
}

Censoring words in string[] by replacing

I am making a censor program for a game .dll I cannot figure out how to do this. I have a string[] of words and sentences. I have found out how to filter the words and block the messages. Right now I am trying to replace words with * the same length as a word. For example if someone said "fuck that stupid ass" it would come out as **** that stupid ***. Below is the code I am using
public void Actionfor(ServerChatEventArgs args)
{
var player = TShock.Players[args.Who];
if (!args.Text.ToLower().StartsWith("/") || args.Text.ToLower().StartsWith("/w") || args.Text.ToLower().StartsWith("/r") || args.Text.ToLower().StartsWith("/me") || args.Text.ToLower().StartsWith("/c") || args.Text.ToLower().StartsWith("/party"))
{
foreach (string Word in config.BanWords)
{
if (player.Group.HasPermission("caw.staff"))
{
args.Handled = false;
}
else if (args.Text.ToLower().Contains(Word))
{
switch (config.Action)
{
case "kick":
args.Handled = true;
TShock.Utils.Kick(player, config.KickMessage, true, false);
break;
case "ignore":
args.Handled = true;
player.SendErrorMessage("Your message has been ignored for saying: {0}", Word);
break;
case "censor":
args.Handled = false;
var wordlength = Word.Length;
break;
case "donothing":
args.Handled = false;
break;
}
}
}
}
else
{
args.Handled = false;
}
}
public string[] BanWords = { "fuck", "ass", "can i be staff", "can i be admin" };
Some places have code something like this under my case "censor"
Word = Word.Replace(Word, new string("*", Word.Length));
However I always get an error cannot convert string to char and cannot figure out else to do.
The compiler is telling you the problem; the overload of String you want takes a char and int, not a string and int.
It's trying to convert the * from a string to a char. Replace the double quotes " with a single quote '.
For chars, use single quotes ' instead of double quotes " like this:
new string('*', Word.Length)
And in your code, you don't need to replace. Simply do:
Word = new string('*', Word.Length);

Send Keys special characters (){}+^ c#

Okay so I'm making an auto typer and I want the user to be able to enter the keys {}()^+ and have the application out put. I know that you need to format the symbols like SendKeys.Send({^}); but I cant get this to work. Heres what I have so far for my Timer Tick. Also, I have global int blockCount, which tells the program to move on to the next character in the blockText string.
It returns "Group delimiters are not balanced."
private void timer3_Tick(object sender, EventArgs e)
{
string blockText = richTextBox1.Text;
int blockLength = richTextBox1.TextLength;
btrand = RandomNumber(75, 200); //I have a method to make a rand num
timer3.Interval = btrand;
char[] specialChars = { '{', '}', '(', ')', '+','^' };
foreach (char letter in blockText)
{
for (int i = 0; i < specialChars.Length; i++)
{
if (letter == specialChars[i])
{
SendKeys.Send("{" + specialChars[i] + "}");
blockText.Remove(blockText.IndexOf(specialChars[i].ToString()));
}
else
{
SendKeys.Send(letter.ToString());
}
}
}
blockCount++;
if (blockCount >= blockLength)
{
blockCount = 0;
}
}
Ok, quick analysis, so forgive me if I miss something.
You're doing a foreach using blockText as your collection, and manipulating it if a special char is found. This can be messy; I would think on another way to implement this.
You're looping through all special chars, and for each interaction that you can't identify a match you're sending the current letter. That means you're re-sending all non-special characters for the number of elements in the specialChars array minus one. I don't think that's what you've intended to do.
I would suggest an implementation like this:
foreach (char letter in blockText)
{
bool _specialCharFound = false;
for (int i = 0; i < specialChars.Length; i++)
{
if (letter == specialChars[i])
{
_specialCharFound = true;
break;
}
}
if (_specialCharFound)
SendKeys.Send("{" + letter.ToString() + "}");
else
SendKeys.Send(letter.ToString());
}
There are more optimized ways to implement, but I would choose this one out of clarity of purpose and similarity to your original code.

What is the best algorithm for arbitrary delimiter/escape character processing?

I'm a little surprised that there isn't some information on this on the web, and I keep finding that the problem is a little stickier than I thought.
Here's the rules:
You are starting with delimited/escaped data to split into an array.
The delimiter is one arbitrary character
The escape character is one arbitrary character
Both the delimiter and the escape character could occur in data
Regex is fine, but a good-performance solution is best
Edit: Empty elements (including leading or ending delimiters) can be ignored
The code signature (in C# would be, basically)
public static string[] smartSplit(
string delimitedData,
char delimiter,
char escape) {}
The stickiest part of the problem is the escaped consecutive escape character case, of course, since (calling / the escape character and , the delimiter): ////////, = ////,
Am I missing somewhere this is handled on the web or in another SO question? If not, put your big brains to work... I think this problem is something that would be nice to have on SO for the public good. I'm working on it myself, but don't have a good solution yet.
A simple state machine is usually the easiest and fastest way. Example in Python:
def extract(input, delim, escape):
# states
parsing = 0
escaped = 1
state = parsing
found = []
parsed = ""
for c in input:
if state == parsing:
if c == delim:
found.append(parsed)
parsed = ""
elif c == escape:
state = escaped
else:
parsed += c
else: # state == escaped
parsed += c
state = parsing
if parsed:
found.append(parsed)
return found
void smartSplit(string const& text, char delim, char esc, vector<string>& tokens)
{
enum State { NORMAL, IN_ESC };
State state = NORMAL;
string frag;
for (size_t i = 0; i<text.length(); ++i)
{
char c = text[i];
switch (state)
{
case NORMAL:
if (c == delim)
{
if (!frag.empty())
tokens.push_back(frag);
frag.clear();
}
else if (c == esc)
state = IN_ESC;
else
frag.append(1, c);
break;
case IN_ESC:
frag.append(1, c);
state = NORMAL;
break;
}
}
if (!frag.empty())
tokens.push_back(frag);
}
private static string[] Split(string input, char delimiter, char escapeChar, bool removeEmpty)
{
if (input == null)
{
return new string[0];
}
char[] specialChars = new char[]{delimiter, escapeChar};
var tokens = new List<string>();
var token = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
var c = input[i];
if (c.Equals(escapeChar))
{
if (i >= input.Length - 1)
{
throw new ArgumentException("Uncompleted escape sequence has been encountered at the end of the input");
}
var nextChar = input[i + 1];
if (nextChar != escapeChar && nextChar != delimiter)
{
throw new ArgumentException("Unknown escape sequence has been encountered: " + c + nextChar);
}
token.Append(nextChar);
i++;
}
else if (c.Equals(delimiter))
{
if (!removeEmpty || token.Length > 0)
{
tokens.Add(token.ToString());
token.Length = 0;
}
}
else
{
var index = input.IndexOfAny(specialChars, i);
if (index < 0)
{
token.Append(c);
}
else
{
token.Append(input.Substring(i, index - i));
i = index - 1;
}
}
}
if (!removeEmpty || token.Length > 0)
{
tokens.Add(token.ToString());
}
return tokens.ToArray();
}
The implementation of this kind of tokenizer in terms of a FSM is fairly straight forward.
You do have a few decisions to make (like, what do I do with leading delimiters? strip or emit NULL tokens).
Here is an abstract version which ignores leading and multiple delimiters, and doesn't allow escaping the newline:
state(input) action
========================
BEGIN(*): token.clear(); state=START;
END(*): return;
*(\n\0): token.emit(); state=END;
START(DELIMITER): ; // NB: the input is *not* added to the token!
START(ESCAPE): state=ESC; // NB: the input is *not* added to the token!
START(*): token.append(input); state=NORM;
NORM(DELIMITER): token.emit(); token.clear(); state=START;
NORM(ESCAPE): state=ESC; // NB: the input is *not* added to the token!
NORM(*): token.append(input);
ESC(*): token.append(input); state=NORM;
This kind of implementation has the advantage of dealing with consecutive excapes naturally, and can be easily extended to give special meaning to more escape sequences (i.e. add a rule like ESC(t) token.appeand(TAB)).
Here's my ported function in C#
public static void smartSplit(string text, char delim, char esc, ref List<string> listToBuild)
{
bool currentlyEscaped = false;
StringBuilder fragment = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (currentlyEscaped)
{
fragment.Append(c);
currentlyEscaped = false;
}
else
{
if (c == delim)
{
if (fragment.Length > 0)
{
listToBuild.Add(fragment.ToString());
fragment.Remove(0, fragment.Length);
}
}
else if (c == esc)
currentlyEscaped = true;
else
fragment.Append(c);
}
}
if (fragment.Length > 0)
{
listToBuild.Add(fragment.ToString());
}
}
Hope this helps someone in the future. Thanks to KenE for pointing me in the right direction.
Here's a more idiomatic and readable way to do it:
public IEnumerable<string> SplitAndUnescape(
string encodedString,
char separator,
char escape)
{
var inEscapeSequence = false;
var currentToken = new StringBuilder();
foreach (var currentCharacter in encodedString)
if (inEscapeSequence)
{
currentToken.Append(currentCharacter);
inEscapeSequence = false;
}
else
if (currentCharacter == escape)
inEscapeSequence = true;
else
if (currentCharacter == separator)
{
yield return currentToken.ToString();
currentToken.Clear();
}
else
currentToken.Append(currentCharacter);
yield return currentToken.ToString();
}
Note that this doesn't remove empty elements. I don't think that should be the responsibility of the parser. If you want to remove them, just call Where(item => item.Any()) on the result.
I think this is too much logic for a single method; it gets hard to follow. If someone has time, I think it would be better to break it up into multiple methods and maybe its own class.
You'ew looking for something like a "string tokenizer". There's a version I found quickly that's similar. Or look at getopt.

Categories

Resources