© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
29 replies
Muhammad Hammad

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();
        }
    }
        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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ The code doesnt work as intended
C#CC# / help
2y ago
why this doesn't work
C#CC# / help
2y ago
✅ why doesn't this work?
C#CC# / help
3y ago
❔ ✅ Why doesn't this basic HttpClient GET request doesn't work as expected?
C#CC# / help
3y ago