Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I tried this it gives the correct num of character but not give the num of word.

What I have tried:

C#
if (txtStringName.Value != " ")
{
    str = txtStringName.Value.ToString().Trim();
    for (i = 0; i != str.Length; i++)
    {
        ch++;
        if (str == " ")
            word++;
    }
}
Posted
Updated 2-Jun-16 23:44pm
v2
Comments
Mehdi Gholam 3-Jun-16 4:48am    
Split on spaces for words then count the characters.
shukla parth 3-Jun-16 4:50am    
how????
Mehdi Gholam 3-Jun-16 4:51am    
Lookup string.Split()
shukla parth 3-Jun-16 4:53am    
i cant understand
phil.o 3-Jun-16 7:45am    
Google -> type string.Split in the search bar -> press enter

Just for the fun of it you can do it with two regular expressions.
C#
int wordCount = Regex.Matches(txtStringName.Value, "\\w+").Count;
int charCount = Regex.Matches(txtStringName.Value, "\\w").Count;
 
Share this answer
 
Comments
shukla parth 3-Jun-16 7:43am    
hello i do all thing but if i put double space it count as word
i mean i write asd asd
it shows 3 word
George Jonsson 3-Jun-16 9:10am    
That's weird. It works for me with that string, 2 word and 6 characters.
What kind of animal is txtStringName.Value?
Is it s string or an object?
Maybe you have to write txtStringName.Value.ToString()

Nah, that shouldn't matter. Forget that

If you use the debugger, what is the string value of txtStringName.Value?
Sergey Alexandrovich Kryukov 3-Jun-16 16:45pm    
5ed. This is the only reasonable answer so far. And this is not because of your use of Regex, this is because other do all those check if string is not " " or "" (as if " " or " " doesn't count) and other pointless things, such as splitting by " " (and what to do with "word, word" then?)...
—SA
George Jonsson 3-Jun-16 22:41pm    
Thanks Sergey.
It is just one way to do it and it is pretty straightforward and simple.
Hello, refer this code,
C#
int NoOfWords = 0;
int NoOfChars = 0;
string objText = textBox1.Text.Trim();

string[] words = objText.Split(' ');
NoOfWords = words.Length;

foreach (string word in words)
{
    NoOfChars += word.Length;
}

MessageBox.Show(string.Format("Words {0} and Chars {1}", NoOfWords, NoOfChars));

Just do some analysis, and think logically before implementing, because it is a very simple scenario.
 
Share this answer
 
try this

C#
string Value = "Today is Friday";
       int CharCount = 0;
       int wordsCount = 0;
       if (Value != "")
       {
           wordsCount++;
           for (int i = 0; i < Value.Length; i++)
           {
               CharCount++;
               if (Value[i] == ' ')
                   wordsCount++;
           }
       }

       Console.WriteLine("Char Count = " + CharCount);
       Console.WriteLine("Words Count = " + wordsCount);
       Console.ReadKey();


Simplest way is
C#
CharCount = Value.Length;
 wordsCount = Value.Split(' ').Length;
 
Share this answer
 
Refer Following code to count Char and Word:


C#
var str = string.Empty;
        int ch = 1;
        int word = 0;

        if (txtStringName1.Value != " ")
        {
            str = txtStringName1.Value.ToString().Trim();

            for (int i = 1; i < str.Length; i++)
            {
                ch++;
                if (char.IsWhiteSpace(str[i - 1]) == true)
                {
                    if (char.IsLetterOrDigit(str[i]) == true ||
                        char.IsPunctuation(str[i]))
                    {
                        word++;
                    }
                }
            }
            if (str.Length > 2)
            {
                word++;
            }
        }

        Response.Write( "Char Count:" + ch +"  Word Count:" + word);
    }
 
Share this answer
 

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