C
C#13mo ago
Ahmed

❔ ✅ A word with the length of > 5 will be reversed, e.g. hello -> olleh

Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present. Examples:
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"
my code:
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;



public class Kata
{
public static string SpinWords(string sentence)
{
string[] words = sentence.Split(" ");
foreach (string word in words){
if (word.Length() >= 5){
StringBuilder sb = new StringBuilder(word);
words[word] = sb.Reverse();
}
string connected_string = words.join(' ');
return connected_string;

}

string sen = SpinWords("this is a trial sentence.");
Console.WriteLine(sen);

}
}
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;



public class Kata
{
public static string SpinWords(string sentence)
{
string[] words = sentence.Split(" ");
foreach (string word in words){
if (word.Length() >= 5){
StringBuilder sb = new StringBuilder(word);
words[word] = sb.Reverse();
}
string connected_string = words.join(' ');
return connected_string;

}

string sen = SpinWords("this is a trial sentence.");
Console.WriteLine(sen);

}
}
error :
src/Solution.cs(14,16): error CS1955: Non-invocable member 'string.Length' cannot be used like a method.
src/Solution.cs(16,13): error CS0029: Cannot implicitly convert type 'string' to 'int'
src/Solution.cs(16,24): error CS1061: 'StringBuilder' does not contain a definition for 'Reverse' and no accessible extension method 'Reverse' accepting a first argument of type 'StringBuilder' could be found (are you missing a using directive or an assembly reference?)
src/Solution.cs(18,39): error CS1061: 'string[]' does not contain a definition for 'join' and no accessible extension method 'join' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)
src/Solution.cs(10,24): error CS0161: 'Kata.SpinWords(string)': not all code paths return a value
src/Solution.cs(14,16): error CS1955: Non-invocable member 'string.Length' cannot be used like a method.
src/Solution.cs(16,13): error CS0029: Cannot implicitly convert type 'string' to 'int'
src/Solution.cs(16,24): error CS1061: 'StringBuilder' does not contain a definition for 'Reverse' and no accessible extension method 'Reverse' accepting a first argument of type 'StringBuilder' could be found (are you missing a using directive or an assembly reference?)
src/Solution.cs(18,39): error CS1061: 'string[]' does not contain a definition for 'join' and no accessible extension method 'join' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)
src/Solution.cs(10,24): error CS0161: 'Kata.SpinWords(string)': not all code paths return a value
61 Replies
Ahmed
AhmedOP13mo ago
but i still don't understand.
Jimmacle
Jimmacle13mo ago
what don't you understand?
Ahmed
AhmedOP13mo ago
the error i'm making
Jimmacle
Jimmacle13mo ago
you wrote a bunch of code that isn't valid C#
Ahmed
AhmedOP13mo ago
is it not?
Jimmacle
Jimmacle13mo ago
so going down your list of errors string has a Length property, not a Length() method you cannot convert a string to an int without using int.Parse StringBuilder doesn't have a reverse method, self explanatory string[] doesn't have a join method and finally your method is supposed to return something but doesn't
Ahmed
AhmedOP13mo ago
how do i access the property? i dont think i was trying to convert
Jimmacle
Jimmacle13mo ago
by typing it exactly how the error shows
Ahmed
AhmedOP13mo ago
dang string.Length
Jimmacle
Jimmacle13mo ago
word.Length no parentheses, because it's not a method
Ahmed
AhmedOP13mo ago
fair enough how do i join string[] then
Jimmacle
Jimmacle13mo ago
all your errors mostly boil down to not researching what is actually available for each of these types string.Join("separator", array)
Ahmed
AhmedOP13mo ago
i need to get the index of the word here
words[word] = sb.Reverse();
words[word] = sb.Reverse();
i'll do it myself but how do i reverse a string hello -> olleh
Ahmed
AhmedOP13mo ago
can i reverse via an array put the word into an array and reverse through there
Jimmacle
Jimmacle13mo ago
what does your research tell you?
Ahmed
AhmedOP13mo ago
yes but it doesn't show me how i can cut down from a string into a character set
Jimmacle
Jimmacle13mo ago
the first result of that google search tells you exactly how
Ahmed
AhmedOP13mo ago
Stack Overflow
Best way to reverse a string
I've just had to write a string reverse function in C# 2.0 (i.e. LINQ not available) and came up with this: public string Reverse(string text) { char[] cArray = text.ToCharArray(); string
Jimmacle
Jimmacle13mo ago
public static string Reverse( string s )
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
public static string Reverse( string s )
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
Ahmed
AhmedOP13mo ago
i'm here ye a char array how do i turn a char[] array into a string[] array so i can add it back to the original sentence string[] reversed_char = chars.ToString();
Jimmacle
Jimmacle13mo ago
you don't want that
Ahmed
AhmedOP13mo ago
doesn't exist
Jimmacle
Jimmacle13mo ago
look at the method i shared here it does exactly what you want line 1 turns the string into an array of chars line 2 reverses that array line 3 creates a new string from that array
Ahmed
AhmedOP13mo ago
can i just do string reversed_word = string(charArray);
Jimmacle
Jimmacle13mo ago
you tell me
Ahmed
AhmedOP13mo ago
yes
Jimmacle
Jimmacle13mo ago
try it
Ahmed
AhmedOP13mo ago
works
Ahmed
AhmedOP13mo ago
No description
Ahmed
AhmedOP13mo ago
however oh wait now i gotta turn it into an array and then replace hmm
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;


public class Kata
{
public static string SpinWords(string sentence)
{

string[] words = sentence.Split(" ");

foreach (string word in words){

if (word.Length >= 5){

int indexOfWord = Array.IndexOf(words, word);

char[] chars = word.ToCharArray();

Array.Reverse(chars);

string reversed_word = new string(chars);

words[indexOfWord] = reversed_word;

}

}

string final_sentence = word.Join(" ");

return final_sentence;
}


}
using System.Collections.Generic;
using System.Linq;
using System;
using System.Text;


public class Kata
{
public static string SpinWords(string sentence)
{

string[] words = sentence.Split(" ");

foreach (string word in words){

if (word.Length >= 5){

int indexOfWord = Array.IndexOf(words, word);

char[] chars = word.ToCharArray();

Array.Reverse(chars);

string reversed_word = new string(chars);

words[indexOfWord] = reversed_word;

}

}

string final_sentence = word.Join(" ");

return final_sentence;
}


}
string final_sentence = word.Join(" ");
string final_sentence = word.Join(" ");
there's an error here what's wrong with it?
Jimmacle
Jimmacle13mo ago
what does your IDE say is wrong with it
Ahmed
AhmedOP13mo ago
word doesn;t exist in this current context
Jimmacle
Jimmacle13mo ago
correct word only exists inside your loop you can't use it outside your loop
Ahmed
AhmedOP13mo ago
No description
Ahmed
AhmedOP13mo ago
No description
Ahmed
AhmedOP13mo ago
No description
Jimmacle
Jimmacle13mo ago
yes, look up how to correctly join an array of strings
Ahmed
AhmedOP13mo ago
what i wanna return doesn't exist either string joinedString = String.Join(" ", words);
Angius
Angius13mo ago
Well You commented out final_sentence variable
Ahmed
AhmedOP13mo ago
done
Angius
Angius13mo ago
So... the IDE is correct, it does not exist
Ahmed
AhmedOP13mo ago
No description
Ahmed
AhmedOP13mo ago
it does
Angius
Angius13mo ago
Not in this scope $scopes
MODiX
MODiX13mo ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Ahmed
AhmedOP13mo ago
how do i make it exist outside the scope
Angius
Angius13mo ago
int foo;
scope {
foo = 69;
}
Console.Write($"The number is {foo}");
int foo;
scope {
foo = 69;
}
Console.Write($"The number is {foo}");
Jimmacle
Jimmacle13mo ago
that is a solution, but i doubt that's the correct one based on the purpose of the code do you really want to rebuild your whole sentence every time you loop?
Ahmed
AhmedOP13mo ago
no
Jimmacle
Jimmacle13mo ago
so why did you put it in the loop?
Ahmed
AhmedOP13mo ago
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
i removed it from the loop how do i console.WriteLine now
Jimmacle
Jimmacle13mo ago
what do you mean?
Ahmed
AhmedOP13mo ago
If i wanna use it Console.WriteLine(SpinWords("this is a trial sentence"));
Jimmacle
Jimmacle13mo ago
it looks like you know how to use it just fine
Ahmed
AhmedOP13mo ago
no like which scope
Jimmacle
Jimmacle13mo ago
what do you mean that's a single line of code with no variables involved
Ahmed
AhmedOP13mo ago
No description
Jimmacle
Jimmacle13mo ago
you need to qualify it with the class name Kata.SpinWords("...")
Ahmed
AhmedOP13mo ago
thank you :)) !close
Accord
Accord13mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server