How to count the different words of a string in Python

To count the different words of a string in Python you can use the following snippet.

Sample Python

    
input = "Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C. The language provides constructs intended to enable clear programs on both a small and large scale.Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts. Using third-party tools, such as Py2exe or Pyinstaller, Python code can be packaged into standalone executable programs. Python interpreters are available for many operating systems.CPython, the reference implementation of Python, is free and open source software and has a community-based development model, as do nearly all of its alternative implementations. CPython is managed by the non-profit Python Software Foundation."
words = input.split(' ')

wordcounter = {}
for word in words:
    if word in wordcounter:
        wordcounter[word] += 1
    else:
        wordcounter[word] = 1

sortedWords = sorted([(counter,word) for word,counter in wordcounter.items()],reverse=True)
print "Word counts: " + str(sortedWords)
print "Most popular word: " + str(sortedWords[0][1]) + " used " + str(sortedWords[0][0]) + " times."

How to capitalize a string in Python

To capitalize a string in Python you can use the following snippet.

Sample Python

myPhrase = "HEllo wOrld!";
myPhrase = myPhrase.lower();
capChars = []
for word in myPhrase.split(' '):
  if len(word) > 1:
    capChars.append(word[0].upper() + word[1:])
  else:
      capChars.append(word.upper())
capChars = ' '.join(capChars)
print capChars;

Output:

Hello World!

another much simpler approach posted by Gomokoo on Twitter would be

print "HEllo wOrld!".lower().title();

Output:

Hello World!

How to get the wordcount of a string in C# and VB.NET

To get the wordcount of a string in C# and VB.NET you can use the following snippet.

Sample C#

public static int WordCount(string s)
{
	return Regex.Matches(s, @"[\S]+").Count;
}

Sample VB.NET

Public Shared Function WordCount(s As String) As Integer
	Return Regex.Matches(s, "[\S]+").Count
End Function

How to cut a string to full words in C# and VB.NET

To cut a string to full words you can use the following snippet.

Sample C#

public string Chop(string input, int maxLength, string chopChars="...", char spaceChar = ' ')
{
	var strings = input.Split(new[] { spaceChar }, StringSplitOptions.RemoveEmptyEntries);
	if (strings[0].Length > maxLength)
	{
		return strings[0];
	}
	var builder = new StringBuilder();
	foreach (var s in strings)
	{
		if ((builder + s).Length > maxLength)
		{
			return string.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars);
		}
		builder.Append(s + spaceChar);
	}
	return string.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars);
}

Sample VB.NET

Public Function Chop(input As String, maxLength As Integer, Optional chopChars As String = "...", Optional spaceChar As Char = " "C) As String
	Dim strings = input.Split(New () {spaceChar}, StringSplitOptions.RemoveEmptyEntries)
	If strings(0).Length > maxLength Then
		Return strings(0)
	End If
	Dim builder = New StringBuilder()
	For Each s As var In strings
		If (builder + s).Length > maxLength Then
			Return String.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars)
		End If
		builder.Append(s + spaceChar)
	Next
	Return String.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars)
End Function