Just had this Pig Latin problem as "homework". The conditions I have been given are:
For words that begin with consonant sounds, all letters before the initial vowel are placed at the end of the word sequence. Then, ay is added.
For words that begin with vowel sounds move the initial vowel(s) along with the first consonant or consonant cluster to the end of the word and add ay.
For words that have no consonant add way.
Tested with:
Write a method that will convert an English sentence into Pig Latin
That turned into
itewray away ethodmay atthay illway onvertcay anay ishenglay entencesay ointay igpay atinlay
It does what it should with one exception which is not in the rules but I thought about it and I have no idea how I can implement it. The method I created does exactly what the problem is asking but if I try to convert an all consonants word into piglatin it does not work. For example grrrrr into piglatin should be grrrrray.
public static string ToPigLatin(string sentencetext)
{
string vowels = "AEIOUaeiou";
//string cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
List<string> newWords = new List<string>();
foreach (string word in sentencetext.Split(' '))
{
if (word.Length == 1)
{
newWords.Add(word + "way");
}
if (word.Length == 2 && vowels.Contains(word[0]))
{
newWords.Add(word + "ay");
}
if (word.Length == 2 && vowels.Contains(word[1]) && !vowels.Contains(word[0]))
{
newWords.Add(word.Substring(1) + word.Substring(0, 1) + "ay");
}
if (word.Length == 2 && !vowels.Contains(word[1]) && !vowels.Contains(word[0]))
{
newWords.Add(word + "ay");
}
for (int i = 1; i < word.Length; i++)
{
if (vowels.Contains(word[i]) && (vowels.Contains(word[0])))
{
newWords.Add(word.Substring(i) + word.Substring(0, i) + "ay");
break;
}
}
for (int i = 0; i < word.Length; i++)
{
if (vowels.Contains(word[i]) && !(vowels.Contains(word[0])) && word.Length > 2)
{
newWords.Add(word.Substring(i) + word.Substring(0, i) + "ay");
break;
}
}
}
return string.Join(" ", newWords);
}
static void Main(string[] args)
{
//Console.WriteLine("Enter a sentence to convert to PigLatin:");
// string sentencetext = Console.ReadLine();
string pigLatin = ToPigLatin("Write a method that will convert an English sentence into Pig Latin");
Console.WriteLine(pigLatin);
Console.ReadKey();
}
Give this a go:
public static string ToPigLatin(string sentencetext)
{
string vowels = "AEIOUaeiou";
string cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
Func<string, string> toPigLatin = word =>
{
word = word.ToLower();
var result = word;
Func<string, string, (string, string)> split = (w, l) =>
{
var prefix = new string(w.ToArray().TakeWhile(x => l.Contains(x)).ToArray());
return (prefix, w.Substring(prefix.Length));
};
if (!word.Any(w => cons.Contains(w)))
{
result = word + "way";
}
else
{
var (s, e) = split(word, vowels);
var (s2, e2) = split(e, cons);
result = e2 + s + s2 + "ay";
}
return result;
};
return string.Join(" ", sentencetext.Split(' ').Select(x => toPigLatin(x)));
}
The code:
string pigLatin = ToPigLatin("Grrrr Write a method that will convert an English sentence into Pig Latin");
Console.WriteLine(pigLatin);
gives:
grrrray itewray away ethodmay atthay illway onvertcay anay ishenglay entencesay ointay igpay atinlay
Related
I am trying to print out the xth word from a sentence like this:
string phrase = Console.ReadLine();
int wordIndex = Convert.ToInt32(Console.ReadLine());
string currentWord = "";
int currentWordIndex = 1;
for (int i = 0; i < phrase.Length; i++)
{
if (phrase[i] != ' ')
currentWord += phrase[i];
if (phrase[i] == ' ' || i == phrase.Length - 1)
{
if (wordIndex == currentWordIndex)
{
Console.WriteLine(currentWord);
break;
}
currentWord = "";
if (i != phrase.Length - 1)
currentWordIndex++;
}
}
if (wordIndex > currentWordIndex)
Console.WriteLine("N/A");
I want it to work even if there are more spaces between words. Any help, please?
string[] splited = phrase.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
string nth = splited.Length >= n ? splited[n-1] : "N/A";
Console.WriteLine(nth);
using System;
using System.Linq; // !! important
public class Program
{
public static void Main()
{
string phrase = Console.ReadLine();
int wordIndex = Convert.ToInt32(Console.ReadLine());
var result = phrase.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)
.Skip(wordIndex - 1)
.FirstOrDefault();
Console.WriteLine(result ?? "N/A");
}
}
output:
>hello, this is a test
>3
is
Another option, same result
var result = phrase.Split()
.Where(x => !string.IsNullOrWhiteSpace(x))
.Skip(wordIndex - 1)
.FirstOrDefault();
You can simply clean double spaces: while (phrase.IndexOf(" ") != -1) phrase = phrase.Replace(" ", " ");. while loop is necessary in case of 4+ spaces between words. The solution with Split and RemoveEmptyEntries is better, my answer is yet another solution.
I have a string that represent an action,
each arg in the action is seporated by the char ';',
for each arg I want to replace the char ',' with the char '.' but only if the ',' is not between ' char using Regex replace
For example:
1- "ActionName('1,b';1,2)"
2- "ActionName('a,b';1,2;1.2;'1,3')"
Desire result:
1- "ActionName('1,b';1.2)"
2- "ActionName('a,b';1.2;1.2;'1,3')
Conditions:
The ',' can appear multiple times inside a string.
Currntly I split the string for ';' loop over all the parts and each part I split for '\''.
Example Code:
public string Transform(string expression)
{
string newExpression = string.Empty;
string[] expParts = expression.Split(';');
for (int i = 0; i < expParts.Length; i++)
{
string newSubExpression = string.Empty;
string[] subExpParts = expParts[i].Split(new char[] { '\'' });
for (int subIndex = 0; subIndex < subExpParts.Length; subIndex += 2)
{
newSubExpression += subExpParts[subIndex].Replace(',', ".");
if (subIndex < subExpParts.Length - 1)
newSubExpression += "\'" + subExpParts[subIndex + 1] + "\'";
}
newExpression += newSubExpression;
if (i < expParts.Length - 1)
newExpression = newExpression + ",";
}
return newExpression;
}
You can use (?<=^([^']|'[^']*')*),
var myPattern= "(?<=^([^']|'[^']*')*),";
var regex = new Regex(myPattern);
var result = regex.Replace("ActionName('a,b';1,2;1.2;'1,3')", ".");
Output
ActionName('a,b';1.2;1.2;'1,3')
Demo here
Since you have tagged the question a regex, I post a regex that works for your input (at least what you posted):
(,(?![\w\d]*'))
Just an example, I think that it can be useful for you as a starting point...
You need to replace the matching regex with a ., in C# you can do like this:
result = Regex.Replace(input, #"(,(?![\w\d]*'))", #".");
Take a look at regex lookaround documentation for more information.
A simple FSM (Finite State Machine) will do. Please, notice that we have just two states (encoded with inQuotation): are we within quotated chunk or not.
public static string Transform(string expression) {
if (string.IsNullOrEmpty(expression))
return expression; // Or throw ArgumentNullException
StringBuilder sb = new StringBuilder(expression.Length);
bool inQuotation = false;
foreach (char c in expression)
if (c == ',' && !inQuotation)
sb.Append('.');
else {
if (c == '\'')
inQuotation = !inQuotation;
sb.Append(c);
}
return sb.ToString();
}
Tests:
string[] tests = new string[] {
"ActionName('1,b';1,2)",
"ActionName('a,b';1,2;1.2;'1,3')",
};
var result = tests
.Select((line, index) => $"{index + 1}- {Transform(line)}");
Console.WriteLine(string.Join(Environment.NewLine, result));
Outcome:
1- ActionName('1,b';1.2)
2- ActionName('a,b';1.2;1.2;'1,3')
i have a string that gets generated and looks like this:
word1:word2:word3:word4
the words i want to find out a seperated by ":"
so i coded the following:
string word1 = "";
string word2 = "";
string word3 = "";
string word4 = "";
int part = 1;
int last_sign = 0;
for (int i = 0; i < string1.Length; i++)
{
if (string1[i] == ':')
{
if (part == 2)
{
part = part + 1;
}
}
if (part == 1)
{
word1 = word1 + string1[i];
}
if (part == 2)
{
word2 = word2 + string1[i];
}
if (part == 3)
{
word3 = word3 + string1[i];
}
if (part == 4)
{
word4 = word4 + string1[i];
}
if (last_sign == 0)
{
if (string1[i + 2] == (string1.Length - 3)) //this is the sign before the last sign
//error line
{ last_sign = 1; }
}
if (last_sign == 1) //this is the last sign
{ break; }
}
but when i try run it the right,complete words get read in, but i get an error (see above).
i hope some of you can point what i am doing wrong
Use the Split method:
string[] words = string1.Split(':');
Now, you have all the words collected into an array which you can then enumerate upon or index into particular positions etc.
e.g.
foreach (var word in words)
Console.WriteLine(word);
Try following code.
class Program
{
static void Main()
{
// Input string contain separators.
string value1 = "word1:word2:word3:word4";
char[] delimiter1 = new char[] { ':' }; // <-- Split on these
// ... Use StringSplitOptions.None.
string[] array1 = value1.Split(delimiter1,
StringSplitOptions.None);
foreach (string entry in array1)
{
Console.WriteLine(entry);
}
// ... Use StringSplitOptions.RemoveEmptyEntries.
string[] array2 = value1.Split(delimiter1,
StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine();
foreach (string entry in array2)
{
Console.WriteLine(entry);
}
}
}
Simply:
char[] delim = {':'};
string[] words = string1.Split(delim);
Then access the elements of words[]
This question already has answers here:
A faster way of doing multiple string replacements
(8 answers)
Closed 9 years ago.
I want to replace some invalid characters in the name of a file uploaded to my application.
I've searched up to something on the internet and found some complex algorithms to do it, here's one:
public static string RemoverAcentuacao(string palavra)
{
string palavraSemAcento = null;
string caracterComAcento = "áàãâäéèêëíìîïóòõôöúùûüçáàãâÄéèêëíìîïóòõÖôúùûÜç, ?&:/!;ºª%‘’()\"”“";
string caracterSemAcento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC___________________";
if (!String.IsNullOrEmpty(palavra))
{
for (int i = 0; i < palavra.Length; i++)
{
if (caracterComAcento.IndexOf(Convert.ToChar(palavra.Substring(i, 1))) >= 0)
{
int car = caracterComAcento.IndexOf(Convert.ToChar(palavra.Substring(i, 1)));
palavraSemAcento += caracterSemAcento.Substring(car, 1);
}
else
{
palavraSemAcento += palavra.Substring(i, 1);
}
}
string[] cEspeciais = { "#39", "---", "--", "'", "#", "\r\n", "\n", "\r" };
for (int q = 0; q < cEspeciais.Length; q++)
{
palavraSemAcento = palavraSemAcento.Replace(cEspeciais[q], "-");
}
for (int x = (cEspeciais.Length - 1); x > -1; x--)
{
palavraSemAcento = palavraSemAcento.Replace(cEspeciais[x], "-");
}
palavraSemAcento = palavraSemAcento.Replace("+", "-").Replace(Environment.NewLine, "").TrimStart('-').TrimEnd('-').Replace("<i>", "-").Replace("<-i>", "-").Replace("<br>", "").Replace("--", "-");
}
else
{
palavraSemAcento = "indefinido";
}
return palavraSemAcento.ToLower();
}
There's a way to do it with a less complex algorithm?
I think this algorithm is very complex to something not too complex, but I can't think in something diferent of this.
I want to replace some invalid characters in the name of a file
if this is really what you want then it is easy
string ToLegalFileName(string s)
{
var invalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());
return String.Join("", s.Select(c => invalidChars.Contains(c) ? '_' : c));
}
if your intent is to replace accented chars with their ascii counterparts then
string RemoverAcentuacao(string s)
{
return String.Join("",
s.Normalize(NormalizationForm.FormD)
.Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark));
}
and this is the 3rd version which replaces accented chars + other chars with '_'
string RemoverAcentuacao2(string s)
{
return String.Join("",
s.Normalize(NormalizationForm.FormD)
.Where(c => char.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
.Select(c => char.IsLetterOrDigit(c) ? c : '_')
.Select(c => (int)c < 128 ? c : '_'));
}
A solution using regular expressions:
string ReplaceSpecial(string input, string replace, char replacewith)
{
char[] back = input.ToCharArray();
var matches = Regex.Matches(String.Format("[{0}]", replace), input);
foreach (var i in matches)
back[i.Index] = replacewith;
return new string(back);
}
A somewhat simpler solution using String.Replace:
string ReplaceSpecial(string input, char[] replace, char replacewith)
{
string back = input;
foreach (char i in replace)
back.Replace(i, replacewith);
return back;
}
static string RemoverAcentuacao(string s)
{
string caracterComAcento = "áàãâäéèêëíìîïóòõôöúùûüçáàãâÄéèêëíìîïóòõÖôúùûÜç, ?&:/!;ºª%‘’()\"”“";
string caracterSemAcento = "aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC___________________";
return new String(s.Select(c =>
{
int i = caracterComAcento.IndexOf(c);
return (i == -1) ? c : caracterSemAcento[i];
}).ToArray());
}
Here is a really simple method that I've used recently.
I hope it meets your requirements. To be honest, the code is a bit difficult to read due to the language of the variable declarations.
List<char> InvalidCharacters = new List<char>() { 'a','b','c' };
static string StripInvalidCharactersFromField(string field)
{
for (int i = 0; i < field.Length; i++)
{
string s = new string(new char[] { field[i] });
if (InvalidCharacters.Contains(s))
{
field = field.Remove(i, 1);
i--;
}
}
return field;
}
Consider the following code:
string[] s = new[] { "Rob", "Jane", "Freddy" };
string joined = string.Join(", ", s);
// joined equals "Rob, Jane, Freddy"
For UI reasons I might well want to display the string "Rob, Jane or Freddy".
Any suggestions about the most concise way to do this?
Edit
I am looking for something that is concise to type. Since I am only concatenating small numbers (<10) of strings I am not worried about run-time performance here.
Concise meaning to type? or to run? The fastest to run will be hand-cranked with StringBuilder. But to type, probably (edit handle 0/1 etc):
string joined;
switch (s.Length) {
case 0: joined = ""; break;
case 1: joined = s[0]; break;
default:
joined = string.Join(", ", s, 0, s.Length - 1)
+ " or " + s[s.Length - 1];
break;
}
The StringBuilder approach might look something like:
static string JoinOr(string[] values) {
switch (values.Length) {
case 0: return "";
case 1: return values[0];
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.Length - 2; i++) {
sb.Append(values[i]).Append(", ");
}
return sb.Append(values[values.Length-2]).Append(" or ")
.Append(values[values.Length-1]).ToString();
}
Concatenate all but the last one. Do the last one manually.
Create an extension method on string[] that implement the same logic as string.Join but the last item will be appended with "or".
string[] s = new[] { "Rob", "Jane", "Freddy" };
Console.WriteLine(s.BetterJoin(", ", " or "));
// ---8<----
namespace ExtensionMethods
{
public static class MyExtensions
{
public static string BetterJoin(this string[] items, string separator, string lastSeparator)
{
StringBuilder sb = new StringBuilder();
int length = items.Length - 2;
int i = 0;
while (i < length)
{
sb.AppendFormat("{0}{1}", items[i++], separator);
}
sb.AppendFormat("{0}{1}", items[i++], lastSeparator);
sb.AppendFormat("{0}", items[i]);
return sb.ToString();
}
}
}
What about:
if (s.Length > 1)
{
uiText = string.Format("{0} and {1}", string.Join(", ", s, 0, s.Length - 1), s[s.Length - 1]);
}
else
{
uiText = s.Length > 0 ? s[0] : "";
}
Generic solution for any type T.
static class IEnumerableExtensions
{
public static string Join<T>(this IEnumerable<T> items,
string seperator, string lastSeperator)
{
var sep = "";
return items.Aggregate("", (current, item) =>
{
var result = String.Concat(current,
// not first OR not last
current == "" || !items.Last().Equals(item) ? sep : lastSeperator,
item.ToString());
sep = seperator;
return result;
});
}
}
Usage:
var three = new string[] { "Rob", "Jane", "Freddy" };
var two = new string[] { "Rob", "Jane" };
var one = new string[] { "Rob" };
var threeResult = three.Join(", ", " or "); // = "Rob, Jane or Freddy"
var twoResult = two.Join(", ", " or "); // = "Rob or Jane"
var oneResult = one.Join(", ", " or "); // = "Rob"
The most memory efficient and scalable would be using a StringBuilder and precalculating the length of the final string to elliminate buffer reallocations. (This is similar to how the String.Concat method works.)
public static string Join(string[] items, string separator, string lastSeparator) {
int len = separator.Length * (items.Length - 2) + lastSeparator.Length;
foreach (string s in items) len += s.Length;
StringBuilder builder = new StringBuilder(len);
for (int i = 0; i < items.Length; i++) {
builder.Append(items[i]);
switch (items.Length - i) {
case 1: break;
case 2: builder.Append(lastSeparator); break;
default: builder.Append(separator); break;
}
}
return builder.ToString();
}
Usage:
string joined = Join(s, ", ", " or ");
An interresting solution would be using a recursive algorithm. It works well for a reasonably small number of strings, but it doesn't scale very well.
public static string Join(string[] items, int index , string separator, string lastSeparator) {
return items[index++] + (index == items.Length-1 ? lastSeparator + items[index] : separator + Join(items, index, separator, lastSeparator));
}
Usage:
string joined = Join(s, 0, ", ", " or ");
string[] name_storage = new[] { "emre" , "balc" };
name_storage[name_storage.Count() - 1] += "ı";
string name = name_storage[0];
string sur_name = name_storage[1];
divElement.InnerHtml += name + " - " + sur_name;
//result = emre - balcı