I'm working on this task on CodeWars: https://www.codewars.com/kata/5667e8f4e3f572a8f2000039/train/csharp.
My code should turn string like this "ZpglnRxqenU" in something like this "Z-Pp-Ggg-Llll-Nnnnn-Rrrrrr-Xxxxxxx-Qqqqqqqq-Eeeeeeeee-Nnnnnnnnnn-Uuuuuuuuuuu".
But I'm getting an error: "Z-Pp-Ggg-Lll-...". It returns three letters "l" instead of four letters.
I tried this code on my PC and the result is the same. But when I use debugger it shows the correct result. How can that be?
Here's my code:
using System;
public class Accumul
{
public static String Accum(string s)
{
string result = "";
for (int i = 0; i < s.Length; i++)
{
result += char.ToUpper(s[i]);
for (int j = 0; j < i; j++)
{
result += char.ToLower(s[i]);
}
result += "-";
}
result = result.Remove(s.Length - 1, 1);
return result;
}
}
You are probably checking result when you debug.
But the error is only at the end result = result.Remove(s.Length - 1, 1);
s.Length - 1 removes the 10th character, you need result.Length -1.
Related
I have tried this:
using System;
using System.Collections;
using System.Collections.Generic;
public class HelloWorld
{
public static string reverseWords(string str){
ArrayList strArr = new ArrayList();
int start = 0;
string revStr = "";
for(int i = 0; i < str.Length; i++){
if(str[i] == ' '){ // if there's a space,
while(start <= str[i - 1]){ // loop thru the iterated values before space
strArr.Add(str[start]); // add them to the ArrayList
start++; // increment `start` until all iterated values are-
} // stored and also for the next word to loop thru
}
}
for(int j = strArr.Count - 1; j >= 0; j--){
revStr += strArr[j] + " "; // keep appending ArrayList values to the-
} // string from the last to the first value
return revStr;
}
public static void Main(string[] args)
{
Console.WriteLine(reverseWords("Our favorite color is Pink"));
//Expected output : Pink is color favorite Our
}
}
And it's giving this error:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
Please help me understand why this is not working. And also, if there's better way to do this ReverseWord function manually(not using any built-in functions at all).
I'm sorry if this is such a noob question. Any constructive criticism is appreciated. Thanks!
Here is a little improved version of your code that actually works for what you are willing to do.
using System;
using System.Collections;
public class HelloWorld
{
public static string reverseWords(string str){
ArrayList strArr = new ArrayList();
string currentWordString = string.Empty;
string revStr = string.Empty;
for(int i = 0; i < str.Length; i++){
if(str[i] == ' '){ // if there's a space,
strArr.Add(currentWordString); // add the accumulated word to the array
currentWordString = string.Empty; // reset accumulator to be used in next iteration
}else {
currentWordString += str[i]; // accumulate the word
}
}
strArr.Add(currentWordString); // add last word to the array
for(int j = strArr.Count - 1; j >= 0; j--){
revStr += strArr[j] + " "; // keep appending ArrayList values to the-
} // string from the last to the first value
return revStr;
}
public static void Main(string[] args)
{
Console.WriteLine(reverseWords("Our favorite color is Pink"));
//Expected output : Pink is color favorite Our
}
}
I'll let you do the remaining. Like removing the trainling space at the end of the sentence. add seperators other than space (e.g comma, semicolons...)
Try this
"Our favorite color is Pink".Split('\u0020').Reverse().ToList().ForEach(x =>
{
Console.WriteLine(x);
});
This will help
public static string ReverseCharacters(string str)
{
if(str == null)
{
throw new ArgumentNullException(nameof(str));
}
int lastIndex = str.Length - 1;
char[] chars = new char[str.Length];
char temp;
for(int i = 0; i < str.Length/2+1; i++)
{
// Swap. You could refactor this to its own method if needed
temp = str[i];
chars[i] = str[lastIndex - i];
chars[lastIndex - i] = temp;
}
return new string(chars);
}
public static string ReverseWords(string str)
{
if (str == null)
{
throw new ArgumentNullException(nameof(str));
}
if (string.IsNullOrWhiteSpace(str))
{
return str;
}
string space = " ";
StringBuilder reversed = new StringBuilder();
// reverse every characters
var reversedCharacters = ReverseCharacters(str);
// split words (space being word separator here)
var reversedWords = reversedCharacters.Split(space);
// for every revered word characters, reverse it back one more time and append.
foreach(var reversedWord in reversedWords)
{
reversed.Append(ReverseCharacters(reversedWord)).Append(space);
}
// remove last extra space
reversed = reversed.Remove(reversed.Length - 1, 1);
return reversed.ToString();
}
Here is the test result:
I am trying to spilt a string word into two strings based on the letter position. The two strings are even and odd. I manage to read the string and used a for loop but the conditional operator is not working and give me the error below. What did I do wrong?
Example: The string word is pole
Even position string - oe
Odd position string - pl
Error
Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
string word = Console.ReadLine();
for(int i = 0; i < word.Length; i++)
{
string even = "";
string odd = "";
((i % 2 == 0) ? even += word[i]: odd += word[i]);
}
You could use the discard operator as the following.
string word = Console.ReadLine();
string even = "";
string odd = "";
for(int i = 0; i < word.Length; i++)
{
var _ = ((i % 2 == 0) ? even += word[i]: odd += word[i]);
}
Couple of points to note here.
You need to declare the odd,even variables outside the loop, otherwise it would be recreated for each iteration of loop
Remember the string is immutable.You could also consider the StringBuilder class.
I am not that familiar with the ? operator, however, in my research, it appears it wants something like below…
((i % 2 == 0) ? ref even : ref odd) += word[i];
Unfortunately, even with this change, the even and odd variables are getting “reset” to empty with each iteration of the for loop with…
string even = "";
string odd = "";
If the goal is to concatenate the values, you do NOT want to create new even and odd variables with each iteration. So you should move those declarations “outside” the for loop. Something like…
string word = Console.ReadLine();
string even = "";
string odd = "";
for (int i = 0; i < word.Length; i++) {
((i % 2 == 0) ? ref even : ref odd) += word[i];
}
You use conditional operator to assign values inside of it. It is not allowed.
The correct for-loop is:
for (int i = 0; i < word.Length; i++)
{
if (i % 2 == 0)
{
even += word[i];
}
else
{
odd += word[i];
};
}
You can also use LINQ to get the expected result:
string word = Console.ReadLine();
string even = string.Concat(word.Where((c,i) => i % 2 == 0));
string odd = string.Concat(word.Where((c,i) => i % 2 == 1));
Online demo: https://dotnetfiddle.net/ePWHnp
This code is supposed to take two samples one original and one new, then determine the length of the smallest single consecutive piece that has been inserted into the first sequence.
When trying some samples I get the following error message:
System.ArgumentOutOfRangeException: 'Index and length must refer to a
place within the string. Parameter name: length
Here is the code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetSample());
Console.ReadKey();
}
public static int GetSample()
{
string sample1 = Console.ReadLine();
string sample2 = Console.ReadLine();
if (sample1 == sample2) return 0;
if (sample1.Length >= sample2.Length)
{
for (int i = 0; i < sample2.Length; i++)
{
if (!(sample1[i] == sample2[i]))
{
sample1 = sample1.Substring(i, sample1.Length);
sample2 = sample2.Substring(i, sample2.Length);
break;
}
}
int var = sample1.Length - sample2.Length;
for (int i = sample2.Length - 1; i >= 0; i--)
{
if (sample2[i] == sample1[i + var])
sample2 = trimlast(sample2);
}
}
else
{
for (int i = 0; i < sample1.Length; i++)
{
if (!(sample1[i] == sample2[i]))
{
sample1 = sample1.Substring(i, sample1.Length);
sample2 = sample2.Substring(i, sample2.Length);
break;
}
}
int var = sample2.Length - sample1.Length;
for (int i = sample1.Length - 1; i >= 0; i--)
{
if (sample2[i + var] == sample1[i])
sample2 = trimlast(sample2);
}
}
return sample2.Length;
}
public static string trimlast(string str)
{
return str.Substring(0, str.Length - 1);
}
}
}
The problem is:
sample1 = sample1.Substring(i, sample1.Length);
and the other similar method calls. The second parameter of Substring is the length (i.e. the number of characters to retrieve for the substring). Thus, if i is greater than 0, it should fail in this case because the method will try to retrieve characters that aren't in the string.
One of your loops is trying to access an element that doesn't exist. For example you have an array a ={1,2,3}, you are trying to access the fourth element, which doesn't exist.
If you can't find out where exactly, there could be an issue, try using print statements inside your loops, displaying the counter (i) values. It will point out for which iteration, your code is failing.
I have some kind of logical error in my program. Whenever I enter a phrase with 1 letter I get a ArgumentOutOfRange Exception, and whenever I enter a multiple letter word the textbox clears, displays "Apple" (the first value in my array) and does nothing else. Can anybody see the logical error in this?
string[] d = { "Apple", "Bass", "Cat", "Dog", "Ear", "Flamingo", "Gear", "Hat", "Infidel", "Jackrabbit", "Kangaroo", "Lathargic", "Monkey", "Nude", "Ozzymandis", "Python", "Queen", "Rat", "Sarcastic", "Tungston", "Urine", "Virginia", "Wool", "Xylophone", "Yo-yo", "Zebra", " " };
string var;
int len = 0;
private void button1_Click(object sender, EventArgs e)
{
var = textBox2.Text;
textBox1.Text = "";
for (int y = 0; y < var.Length; y++)
{
for (int x = 0; x < d.Length; x++)
{
if (d[x].ToUpper().Substring(0, 0) == var.ToUpper().Substring(len, len))
{
len = len + 1;
textBox1.Text = textBox1.Text + "\n" + d[x];
}
}
}
}
Substring(0, 0) is really pointless. This will always be an empty string.
Substring(len, len) also is a bad idea, because it will return a string of length len starting at index len. This is where you get your exception.
I assume, what you really want is the second parameter to be 1 in both cases. And that can be further simplified to an access via index:
d[x].ToUpper()[0] == var.ToUpper()[len]
You can do the same quite easily using LINQ:
private void button1_Click(object sender, EventArgs e)
{
var dict = d.ToDictionary(x => x.First(), x => x);
textBox1.Text = string.Join(Environment.NewLine, textBox2.Text.Select(x => dict[char.ToUpper(x)]));
}
To do it without LINQ I would suggest following:
for (int y = 0; y < input.Length; y++)
{
for (int x = 0; x < d.Length; x++)
{
if (char.ToUpper(d[x][0]) == char.ToUpper(input[y]))
{
result = result + "\n" + d[x];
}
}
}
Changes are:
you don't need len variable. Use y instead.
you don't need whole string uppercased. Use char.ToUpper static method instead.
you don't need string.Substring method. Use indexers instead.
This line explains the behavior.
if (d[x].ToUpper().Substring(0, 0) == var.ToUpper().Substring(len, len))
Second parameter of substring is string length. So on the left you always have an empty string. On the right you also have empty string when len==0 (that's why your code always picks Apple).
After that you change len, and repeat the loop. Then expression on the right is var.Substring(1,1) which gives you the error if your string is 1 character long. Because this reads - 1 symbol starting with 1 (which is second character of the string)
Apart from that, the purpose of the code is complete mystery, so there are definitely other errors.
_documentContent contains the whole document as html view source.
patternToFind contains text to be searched in _documentContent.
Code snippet below works fine if language is English.
The same code however doesn't works at all when it encounters a language like Korean.
Sample Document
Present Tense
The present tense is just as you have learned. You take the dictionary form of a verb, drop the 다, add the appropriate ending.
먹다 - 먹 + 어요 = 먹어요
마시다 - 마시 + 어요 - 마시어요 - 마셔요.
This tense is used to represent what happens in the present. I eat. I drink. It is a general term for the present.
When I am trying to find 먹 the code belows fails.
can someone please suggest some solution to this
using System;
using System.Collections.Generic;
using System.Text;
namespace MultiByteStringHandling
{
class Program
{
static void Main(string[] args)
{
string _documentContent = #"먹다 - 먹 + 어요 = 먹어요";
byte[] patternToFind = Encoding.UTF8.GetBytes("먹");
byte[] DocumentBytes = Encoding.UTF8.GetBytes(_documentContent);
int intByteOffset = indexOf(DocumentBytes, patternToFind);
Console.WriteLine(intByteOffset.ToString());
}
public int indexOf(byte[] data, byte[] pattern)
{
int[] failure = computeFailure(pattern);
int j = 0;
if (data.Length == 0) return 0;
for (int i = 0; i < data.Length; i++)
{
while (j > 0 && pattern[j] != data[i])
{
j = failure[j - 1];
}
if (pattern[j] == data[i])
{
j++;
}
if (j == pattern.Length)
{
return i - pattern.Length + 1;
}
}
return -1;
}
/**
* Computes the failure function using a boot-strapping process,
* where the pattern is matched against itself.
*/
private int[] computeFailure(byte[] pattern)
{
int[] failure = new int[pattern.Length];
int j = 0;
for (int i = 1; i < pattern.Length; i++)
{
while (j > 0 && pattern[j] != pattern[i])
{
j = failure[j - 1];
}
if (pattern[j] == pattern[i])
{
j++;
}
failure[i] = j;
}
return failure;
}
}
}
Seriously, why not just do the following?
var indexFound = documentContent.IndexOf("data");
Converting strings into byte arrays and then searching those doesn't make much sense to me when you're original data is text. You can always find the byte position after if you wish.
UTF-8 is a variable multi-byte format. Searching for English text in Korean data will never match on a direct pattern match. If you are scanning text you would be much better off using .IndexOf(pattern) [as Noldorin pointed out] or .Contains(pattern).