Click here to Skip to main content
12,396,215 members (57,584 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
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.
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 8-Oct-12 1:59am
Comments
ProgramFOX 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can use this code to count the number of words:

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.
  Permalink  
v3
Comments
ridoy 8-Oct-12 11:06am
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Why not try it the simple way?

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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


Change to:

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).
  Permalink  
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).
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
Peeyush Pachaori 10-Oct-12 4:32am
   
Explanation given is correct. Can i know the reason why downvoted?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100