C
C#8mo ago
Muhammad

❔ ✅ 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
Muhammad
Muhammad8mo ago
but i still don't understand.
Jimmacle
Jimmacle8mo ago
what don't you understand?
Muhammad
Muhammad8mo ago
the error i'm making
Jimmacle
Jimmacle8mo ago
you wrote a bunch of code that isn't valid C#
Muhammad
Muhammad8mo ago
is it not?
Jimmacle
Jimmacle8mo 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
Muhammad
Muhammad8mo ago
how do i access the property? i dont think i was trying to convert
Jimmacle
Jimmacle8mo ago
by typing it exactly how the error shows
Muhammad
Muhammad8mo ago
dang string.Length
Jimmacle
Jimmacle8mo ago
word.Length no parentheses, because it's not a method
Muhammad
Muhammad8mo ago
fair enough how do i join string[] then
Jimmacle
Jimmacle8mo ago
all your errors mostly boil down to not researching what is actually available for each of these types string.Join("separator", array)
Muhammad
Muhammad8mo 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
Muhammad
Muhammad8mo ago
can i reverse via an array put the word into an array and reverse through there
Jimmacle
Jimmacle8mo ago
what does your research tell you?
Muhammad
Muhammad8mo ago
yes but it doesn't show me how i can cut down from a string into a character set
Jimmacle
Jimmacle8mo ago
the first result of that google search tells you exactly how
Muhammad
Muhammad8mo 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
Jimmacle8mo 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);
}
Muhammad
Muhammad8mo 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
Jimmacle8mo ago
you don't want that
Muhammad
Muhammad8mo ago
doesn't exist
Jimmacle
Jimmacle8mo 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
Muhammad
Muhammad8mo ago
can i just do string reversed_word = string(charArray);
Jimmacle
Jimmacle8mo ago
you tell me
Muhammad
Muhammad8mo ago
yes
Jimmacle
Jimmacle8mo ago
try it
Muhammad
Muhammad8mo ago
works
Muhammad
Muhammad8mo ago
No description
Muhammad
Muhammad8mo 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
Jimmacle8mo ago
what does your IDE say is wrong with it
Muhammad
Muhammad8mo ago
word doesn;t exist in this current context
Jimmacle
Jimmacle8mo ago
correct word only exists inside your loop you can't use it outside your loop
Muhammad
Muhammad8mo ago
No description
Muhammad
Muhammad8mo ago
No description
Muhammad
Muhammad8mo ago
No description
Jimmacle
Jimmacle8mo ago
yes, look up how to correctly join an array of strings
Muhammad
Muhammad8mo ago
what i wanna return doesn't exist either string joinedString = String.Join(" ", words);
Angius
Angius8mo ago
Well You commented out final_sentence variable
Muhammad
Muhammad8mo ago
done
Angius
Angius8mo ago
So... the IDE is correct, it does not exist
Muhammad
Muhammad8mo ago
No description
Muhammad
Muhammad8mo ago
it does
Angius
Angius8mo ago
Not in this scope $scopes
MODiX
MODiX8mo 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
Muhammad
Muhammad8mo ago
how do i make it exist outside the scope
Angius
Angius8mo ago
int foo;
scope {
foo = 69;
}
Console.Write($"The number is {foo}");
int foo;
scope {
foo = 69;
}
Console.Write($"The number is {foo}");
Jimmacle
Jimmacle8mo 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?
Muhammad
Muhammad8mo ago
no
Jimmacle
Jimmacle8mo ago
so why did you put it in the loop?
Muhammad
Muhammad8mo 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
Jimmacle8mo ago
what do you mean?
Muhammad
Muhammad8mo ago
If i wanna use it Console.WriteLine(SpinWords("this is a trial sentence"));
Jimmacle
Jimmacle8mo ago
it looks like you know how to use it just fine
Muhammad
Muhammad8mo ago
no like which scope
Jimmacle
Jimmacle8mo ago
what do you mean that's a single line of code with no variables involved
Muhammad
Muhammad8mo ago
No description
Jimmacle
Jimmacle8mo ago
you need to qualify it with the class name Kata.SpinWords("...")
Muhammad
Muhammad8mo ago
thank you :)) !close
Accord
Accord8mo 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
More Posts
❔ Error with showing Images in Azure App Service.Using Asp.net and Azure Blob Storage and Azure App Service: When I run my API locally on my computeIssues with EFCoreHello everyone, I am trying to use ApplyConfigurationsFromAssembly but it seems to not be finding an❔ Could u help me plzI´m learning coding with a youtube course, and i´m trying to do a crud, but this error with the form❔ Modular programming ?Hello, I am currently studying video game programming in C#. As part of this, we have a project to ❔ Is this possible in WEBAPI to add logic between JWT token validation and the authorization?I read a blog about the AspNetCore WebApi: https://jasonwatmore.com/post/2022/02/18/net-6-role-based✅ docker build with dotnet 8ERROR: Service 'vmg-dashboards-api' failed to build: The command '/bin/sh -c dotnet restore "vmg.dasMake every textblock in an itemscontrol with an OCollection<string> as the source have a click eventI have an itemscontrl with a OCollection as the source which means it can be updated i have a textbl❔ Validation of appsettings configuration before running the applicationHello. I would like to validate entries in the appsettings configuration at application startup to ❔ Migrate AppDomain (.net framework) to AssemblyLoadContext (.net core)I need help converting this code to the .net core equivalent by using AppDomain here, ensured that t❔ Form goes fully transparent please helpi was trying to make blurry panel then i searched and i found something but the problem is i see squ