Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Sir,

I want to count words without space in a Sentence but I face a problem that when I run the program space is also count with words but I want count only words Please guide . My C# program is as under.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;

namespace Static_Method_Count_Word
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter Any Paragraph .");
            string Paragraph = Console.ReadLine();
            Count.Coun(Paragraph);
            Console.ReadLine();
        }
    }
    class Count
    {
        public static void Coun(string Paragraph )
        {
            int i = 0,b=0;
            int Count2 = 1;
            for (i = 0; i < Paragraph.Length; i++)
            {
                if (Paragraph[i] == ' ')
                {
                    for (b = i; b < Paragraph.Length; b++)
                    {
                        if (Paragraph[b] != ' ' && Paragraph[b] != '\t')
                        {
                            Count2++;
                            break;
                        }
                    }
                }
	     }
	}
     }
}
Posted
Updated 1-Feb-15 8:20am
v2
Comments
Andreas Gieriet 1-Feb-15 14:20pm    
Added code block.
Andi

Two things:
1) use the respective split option to not place the empty parts into the list
2) allow the split function to take char.IsWhitespace(c) as separator
See remarks on null parameter in String.Split Method (Char[], StringSplitOptions)[^].
C#
char[] takeWhiteSpaceSeparators = null;
int wordCount = paragraph.Split( takeWhiteSpaceSeparators
                               , StringSplitOptions.RemoveEmptyEntries
                               )
                               .Length;
Cheers
Andi
 
Share this answer
 
v3
You can try using this code for counting the words,

C#
// Method returns an integer and gets a string paragraph
int GetWords(string paragraph) {
// It returns the total number of words
return (
        // Splits the paragraph at every occurance of empty string
        paragraph.Split(new string[] {" "}, 
        // Spliting options let you omit the empty words, like " "
        StringSplitOptions.RemoveEmptyEntries).Length
       );
}


This would return the total number of words in the paragraph or what so ever the string is. To test this program logic, you can use this fiddle: https://dotnetfiddle.net/E1JIy2[^]. For more on this Split method signature and other details, see this MSDN document[^].
 
Share this answer
 
Comments
Zoltán Zörgő 1-Feb-15 15:03pm    
How does your answer differ form V-s answer?
Andreas Gieriet 1-Feb-15 17:35pm    
You are not taking tabs as separators as the OP has in his code.
I think any whitespace should be considered as with char.IsWhiteSpace(c).
Cheers
Andi
Use the string split method and more accurately this overloaded version is probably the best: see here[^]

You´ll need:
string [] seperator = { " " };
string [] words = Paragraph.Split(seperator, StringSplitOptions.RemoveEmptyEntries);
int nrofwords = words.Length;


hope this helps.
 
Share this answer
 
Comments
Andreas Gieriet 1-Feb-15 17:36pm    
You are not taking tabs as separators as the OP has in his code.
I think any whitespace should be considered as with char.IsWhiteSpace(c).
Cheers
Andi
V. 2-Feb-15 1:24am    
You can add the tab in the seperator array as well as newlines and other whitespace characters ;-)
Andreas Gieriet 2-Feb-15 2:15am    
Why reinventing the wheel?
See MSDN documentation on whitespace characters:
Char.IsWhiteSpace Method:
[...]White space characters are the following Unicode characters:

- Members of the SpaceSeparator category, which includes the characters SPACE (U+0020), OGHAM SPACE MARK (U+1680), EN QUAD (U+2000), EM QUAD (U+2001), EN SPACE (U+2002), EM SPACE (U+2003), THREE-PER-EM SPACE (U+2004), FOUR-PER-EM SPACE (U+2005), SIX-PER-EM SPACE (U+2006), FIGURE SPACE (U+2007), PUNCTUATION SPACE (U+2008), THIN SPACE (U+2009), HAIR SPACE (U+200A), NARROW NO-BREAK SPACE (U+202F), MEDIUM MATHEMATICAL SPACE (U+205F), and IDEOGRAPHIC SPACE (U+3000).

- Members of the LineSeparator category, which consists solely of the LINE SEPARATOR character (U+2028).

- Members of the ParagraphSeparator category, which consists solely of the PARAGRAPH SEPARATOR character (U+2029).

- The characters CHARACTER TABULATION (U+0009), LINE FEED (U+000A), LINE TABULATION (U+000B), FORM FEED (U+000C), CARRIAGE RETURN (U+000D), NEXT LINE (U+0085), and NO-BREAK SPACE (U+00A0).

Cheers
Andi

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