❔ ✅ 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:
my code:
error :
61 Replies
but i still don't understand.
what don't you understand?
the error i'm making
you wrote a bunch of code that isn't valid C#
is it not?
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'thow do i access the property?
i dont think i was trying to convert
by typing it exactly how the error shows
dang
string.Length
word.Length
no parentheses, because it's not a methodfair enough
how do i join string[] then
all your errors mostly boil down to not researching what is actually available for each of these types
string.Join("separator", array)
i need to get the index of the word
here
i'll do it myself but how do i reverse a string
hello -> olleh
can i reverse via an array
put the word into an array
and reverse through there
what does your research tell you?
yes
but it doesn't show me how i can cut down from a string into a character set
the first result of that google search tells you exactly how
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
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();
you don't want that
doesn't exist
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
can i just do
string reversed_word = string(charArray);
you tell me
yes
try it
works
however
oh wait now i gotta turn it into an array and then replace
hmm
there's an error here
what's wrong with it?
what does your IDE say is wrong with it
word doesn;t exist in this current context
correct
word
only exists inside your loop
you can't use it outside your loopyes, look up how to correctly join an array of strings
what i wanna return doesn't exist either
string joinedString = String.Join(" ", words);
Well
You commented out
final_sentence
variabledone
So... the IDE is correct, it does not exist
it does
Not in this scope
$scopes
thing a
is available in scope A
and scope B
thing b
is available only in scope B
how do i make it exist outside the scope
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?
no
so why did you put it in the loop?
i removed it from the loop
how do i console.WriteLine now
what do you mean?
If i wanna use it
Console.WriteLine(SpinWords("this is a trial sentence"));
it looks like you know how to use it just fine
no like
which scope
what do you mean
that's a single line of code with no variables involved
you need to qualify it with the class name
Kata.SpinWords("...")
thank you :))
!close
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.