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 that
22 Replies
Muhammad Hammad
Muhammad HammadOP5mo ago
btw, how to share my code so that it can look readable!
Angius
Angius5mo ago
$code
MODiX
MODiX5mo ago
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/
SG97
SG975mo ago
tried debugging?
Muhammad Hammad
Muhammad HammadOP5mo ago
yep bro
SG97
SG975mo ago
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
Muhammad Hammad
Muhammad HammadOP5mo ago
No description
SG97
SG975mo ago
I mean when you debug and go to that line with that input
Muhammad Hammad
Muhammad HammadOP5mo ago
I am unaware of debugging I just know f5 I'll try
SG97
SG975mo ago
$debug
MODiX
MODiX5mo ago
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.
SG97
SG975mo ago
the most useful thing you'll ever learn
Muhammad Hammad
Muhammad HammadOP5mo ago
No description
SG97
SG975mo ago
yup, and should it add the first a to the words?
Muhammad Hammad
Muhammad HammadOP5mo ago
idk!
SG97
SG975mo ago
I assumed your words just counts the whole words, not characters
Muhammad Hammad
Muhammad HammadOP5mo ago
the "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!
SG97
SG975mo ago
you've found your bug
Muhammad Hammad
Muhammad HammadOP5mo ago
it seems like very helpful
SG97
SG975mo ago
it is
Muhammad Hammad
Muhammad HammadOP5mo ago
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
Muhammad Hammad
Muhammad HammadOP5mo ago
No description
Want results from more Discord servers?
Add your server