seems like this doesn't work as intended!
static void Main(string[] args)
{
Console.WriteLine("Welcome to our Words Counter");
Console.Write("Put your paragraphs here to get words number's count: ");
string userInput = Console.ReadLine();
List<string> words = new List<string>();
string word = "";
for(int i=0; i<=userInput.Length-1;i++)
{
if (userInput[i]==' ')
{
if(!string.IsNullOrWhiteSpace(word))
{
words.Add(word);
word = "";
}
}
else
{
word += userInput[i];
}
//Adds the last word if the input doesn't end with a space
if (!string.IsNullOrWhiteSpace(word))
{
words.Add(word);
}
}
Console.WriteLine($"The ammount of words in your input are: {words.Count}");
Console.ReadLine();
}
}
It just counts character while I made it to count words! unable to spot the issue haven't spent more than 3 minutes to spot but thought of sharing it over here! I am sorry for that22 Replies
btw, how to share my code so that it can look readable!
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/tried debugging?
yep bro
how does it look like at the part
//Adds the last word if the input doesn't end with a space
let's say the input is as a man
I mean when you debug
and go to that line with that input
I am unaware of debugging I just know f5
I'll try
$debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
the most useful thing you'll ever learn
yup, and should it add the first
a
to the words
?idk!
I assumed your
words
just counts the whole words, not charactersthe "word" must contain a whole word and when the program detects a space the word should be added to the list "words" and word must be intilized to ="" then again it goes again
I want to make it count words!
you've found your bug
it seems like very helpful
it is
can you tell me!
oho
damn
the logic to add the last word was inside the for loop! which made it to be added repeatedly! and it was a the first letter of as which was being added repeatedly! thanks SG97