Click here to Skip to main content
15,885,167 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all. i wanna write c# console app that compute " Number of Words in a Sentence"
and i write this code but it's a big problem that if number of "Space" in between sentence more than one the program output is Wrong.

C#
using System;
namespace Test
{
    class Program
    {
        static void Main()
        {
            string st1 = "  this is a test    ";
            // First character
            char firstChar = 'a';
            int counter = 0;
            int indexfirstChar = 0;
            char lastChar = 'l';
            int indexLastChar = 0;
            for (int i = 0; i < st1.Length; i++)
            {
                if (char.IsLetter(st1[i]) || char.IsDigit(st1[i]))
                {
                    firstChar = st1[i];
                    indexfirstChar = i;
                    break;
                }
            }
            // latest character 
            for (int i = st1.Length - 1; i >= 0; i--)
            {
                if (char.IsLetter(st1[i]) || char.IsDigit(st1[i]))
                {
                    lastChar = st1[i];
                    indexLastChar = i;
                    break;
                }
            }
          
            for (int i = indexfirstChar; i < indexLastChar + 1; i++)
            {
                if (st1[i].ToString() == " ")
                    counter++;
            }
            Console.WriteLine(" Number of Word:" + counter + 1);
        }
    }
}
Posted
Comments
Thomas Daniels 8-Oct-12 8:03am    
And what's the output?
AlirezaDehqani 8-Oct-12 8:15am    
output is correct for this program and it's 4. but sentence be this " this is a test" output is wrong.

Quote:
for (int i = indexfirstChar; i < indexLastChar + 1; i++)
{
if (st1[i].ToString() == " ")
counter++;
}


Change to:

C#
bool newspace = false;
for (int i = indexfirstChar; i < indexLastChar + 1; i++)
{
    if (st1[i].ToString() == " ")
    {
      if (! newspace )
      {
         counter++;
         newspace = true;
      }
    }
    else
    {
       if ( newspace )
       {
          newspace = false;
       }
    }
 }


BTW I suppose you should add 1 to the result (we're actually counting blank sequences occurrences).
 
Share this answer
 
v2
Comments
AlirezaDehqani 8-Oct-12 8:29am    
tnx. your code is very better for the program ;)
CPallini 8-Oct-12 8:38am    
You are welcome. BTW you can improve your code (finding first and last characters is not really necessary).
Your program is correct just change line to print on console:

Console.WriteLine(" Number of Word:" + (counter + 1));


In your code : counter + 1 ; 1 has been treated as string literal.
 
Share this answer
 
Comments
Peeyush Pachaori 10-Oct-12 4:32am    
Explanation given is correct. Can i know the reason why downvoted?
Why not try it the simple way?

C#
string source = "This is a sentence, that I want to check     - no, really, I do!";
string[] parts = source.Split(new char[] { ' ', '.', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);

It may need a little tweaking, but it will basically do what you want.
 
Share this answer
 
You can use this code to count the number of words:

C#
using System;
namespace Test {
public class Program
{
   public void Main()
   {
    string st1 = "  this is a test   ";
    char[] seperator = new char[] { ' ' };
    int numberOfWords = st1.Split(seperator,StringSplitOptions.RemoveEmptyEntries).Length + 1;
    Console.WriteLine("Number of words: {0}", numberOfWords);
   }
}
}


Hope this helps.
 
Share this answer
 
v3
Comments
ridoy 8-Oct-12 11:06am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900