Count number of words in a string

Here given code implementation process.

// Java program
// Count number of words in a string
class MyString
{
	//Count number of words in given string
	public int count_words(String str)
	{
		int counter = 0;
		//Loop contolling variables
		int i = 0, j = 0;
		System.out.print("\n Given String : [" + str + "]\n");
		//Loop which is iterate given string character elements
		for (i = 0; i < str.length(); i++)
		{
			if (str.charAt(i) != ' ')
			{
				counter++;
				//loop, find word length
				for (j = i; j < str.length(); j++)
				{
					//Check that given character is space character or not
					if (str.charAt(j) == ' ')
					{
						break;
					}
				}
				//Move to current location
				i = j;
			}
		}
		return counter;
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		// Simple case
		int length = obj.count_words("Hello are you ready");
		System.out.print(" Total Words : " + length + "\n");
		length = obj.count_words("Can if it's possible to do new approach");
		System.out.print(" Total Words : " + length + "\n");
		length = obj.count_words("I have lost        my             job");
		System.out.print(" Total Words : " + length + "\n");
	}
}

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
// C++ program
// Count number of words in a string

//Include header file
#include <iostream>
using namespace std;

class MyString
{
	public:
		//Count number of words in given string
		int count_words(string str)
		{
			int counter = 0;
			//Loop contolling variables
			int i = 0, j = 0;
			cout << "\n Given String : [" << str << "]\n";
			//Loop which is iterate given string character elements
			for (i = 0; i < str.size(); i++)
			{
				if (str[i] != ' ')
				{
					counter++;
					//loop, find word length
					for (j = i; j < str.size(); j++)
					{
						//Check that given character is space character or not
						if (str[j] == ' ')
						{
							break;
						}
					}
					//Move to current location
					i = j;
				}
			}
			return counter;
		}
};
int main()
{
	MyString obj = MyString();
	// Simple case
	int length = obj.count_words("Hello are you ready");
	cout << " Total Words : " << length << "\n";
	length = obj.count_words("Can if it's possible to do new approach");
	cout << " Total Words : " << length << "\n";
	length = obj.count_words("I have lost        my             job");
	cout << " Total Words : " << length << "\n";
	return 0;
}

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
// C# program
// Count number of words in a string

//Include namespace system
using System;
class MyString
{
	//Count number of words in given string
	public int count_words(String str)
	{
		int counter = 0;
		//Loop contolling variables
		int i = 0, j = 0;
		Console.Write("\n Given String : [" + str + "]\n");
		//Loop which is iterate given string character elements
		for (i = 0; i < str.Length; i++)
		{
			if (str[i] != ' ')
			{
				counter++;
				//loop, find word length
				for (j = i; j < str.Length; j++)
				{
					//Check that given character is space character or not
					if (str[j] == ' ')
					{
						break;
					}
				}
				//Move to current location
				i = j;
			}
		}
		return counter;
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		// Simple case
		int length = obj.count_words("Hello are you ready");
		Console.Write(" Total Words : " + length + "\n");
		length = obj.count_words("Can if it's possible to do new approach");
		Console.Write(" Total Words : " + length + "\n");
		length = obj.count_words("I have lost        my             job");
		Console.Write(" Total Words : " + length + "\n");
	}
}

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
<?php
// Php program
// Count number of words in a string
class MyString
{
	//Count number of words in given string
	public	function count_words($str)
	{
		$counter = 0;
		//Loop contolling variables
		$i = 0;
		$j = 0;
		echo "\n Given String : [". $str ."]\n";
		//Loop which is iterate given string character elements
		for ($i = 0; $i < strlen($str); $i++)
		{
			if ($str[$i] != ' ')
			{
				$counter++;
				//loop, find word length
				for ($j = $i; $j < strlen($str); $j++)
				{
					//Check that given character is space character or not
					if ($str[$j] == ' ')
					{
						break;
					}
				}
				//Move to current location
				$i = $j;
			}
		}
		return $counter;
	}
}

function main()
{
	$obj = new MyString();
	// Simple case
	$length = $obj->count_words("Hello are you ready");
	echo " Total Words : ". $length ."\n";
	$length = $obj->count_words("Can if it's possible to do new approach");
	echo " Total Words : ". $length ."\n";
	$length = $obj->count_words("I have lost        my             job");
	echo " Total Words : ". $length ."\n";
}
main();

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
// Node Js program
// Count number of words in a string
class MyString
{
	//Count number of words in given string
	count_words(str)
	{
		var counter = 0;
		//Loop contolling variables
		var i = 0;
		var j = 0;
		process.stdout.write("\n Given String : [" + str + "]\n");
		//Loop which is iterate given string character elements
		for (i = 0; i < str.length; i++)
		{
			if (str[i] != ' ')
			{
				counter++;
				//loop, find word length
				for (j = i; j < str.length; j++)
				{
					//Check that given character is space character or not
					if (str[j] == ' ')
					{
						break;
					}
				}
				//Move to current location
				i = j;
			}
		}
		return counter;
	}
}

function main()
{
	var obj = new MyString();
	// Simple case
	var length = obj.count_words("Hello are you ready");
	process.stdout.write(" Total Words : " + length + "\n");
	length = obj.count_words("Can if it's possible to do new approach");
	process.stdout.write(" Total Words : " + length + "\n");
	length = obj.count_words("I have lost        my             job");
	process.stdout.write(" Total Words : " + length + "\n");
}
main();

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
#  Python 3 program
#  Count number of words in a string
class MyString :
	# Count number of words in given string
	def count_words(self, str) :
		counter = 0
		# Loop contolling variables
		i = 0
		j = 0
		print("\n Given String : [{0}]".format(str))
		# Loop which is iterate given string character elements
		while (i < len(str)) :
			if (str[i] != ' ') :
				counter += 1
				# loop, find word length
				j = i
				while (j < len(str)) :
					# Check that given character is space character or not
					if (str[j] == ' ') :
						break
					
					j += 1
				
				# Move to current location
				i = j
			
			i += 1
		
		return counter
	

def main() :
	obj = MyString()
	#  Simple case
	length = obj.count_words("Hello are you ready")
	print(" Total Words : ", length )
	length = obj.count_words("Can if it's possible to do new approach")
	print(" Total Words : ", length )
	length = obj.count_words("I have lost        my             job")
	print(" Total Words : ", length )

if __name__ == "__main__": main()

Output

 Given String : [Hello are you ready]
 Total Words :  4

 Given String : [Can if it's possible to do new approach]
 Total Words :  8

 Given String : [I have lost        my             job]
 Total Words :  5
#  Ruby program
#  Count number of words in a string
class MyString

	# Count number of words in given string
	def count_words(str)
	
		counter = 0
		# Loop contolling variables
		i = 0
		j = 0
		print("\n Given String : [", str ,"]\n")
		# Loop which is iterate given string character elements
		while (i < str.length())
		
			if (str[i] != ' ')
			
				counter += 1
				# loop, find word length
				j = i
				while (j < str.length())
				
					# Check that given character is space character or not
					if (str[j] == ' ')
					
						break
					end
					j += 1
				end
				# Move to current location
				i = j
			end
			i += 1
		end
		return counter
	end
end
def main()

	obj = MyString.new()
	#  Simple case
	length = obj.count_words("Hello are you ready")
	print(" Total Words : ", length ,"\n")
	length = obj.count_words("Can if it's possible to do new approach")
	print(" Total Words : ", length ,"\n")
	length = obj.count_words("I have lost        my             job")
	print(" Total Words : ", length ,"\n")
end
main()

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
import scala.util.control.Breaks._
// Scala program
// Count number of words in a string
class MyString
{
	//Count number of words in given string
	def count_words(str: String): Int = {
		var counter: Int = 0;
		//Loop contolling variables
		var i: Int = 0;
		var j: Int = 0;
		print("\n Given String : [" + str + "]\n");
		//Loop which is iterate given string character elements
		while (i < str.length())
		{
			if (str(i) != ' ')
			{
				counter += 1;
				//loop, find word length
				j = i;
              	breakable {
				while (j < str.length())
				{
					//Check that given character is space character or not
					if (str(j) == ' ')
					{
						break;
					}
					j += 1;
				}
                }
				//Move to current location
				i = j;
			}
			i += 1;
		}
		return counter;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		// Simple case
		var length: Int = obj.count_words("Hello are you ready");
		print(" Total Words : " + length + "\n");
		length = obj.count_words("Can if it's possible to do new approach");
		print(" Total Words : " + length + "\n");
		length = obj.count_words("I have lost        my             job");
		print(" Total Words : " + length + "\n");
	}
}

Output

 Given String : [Hello are you ready]
 Total Words : 4

 Given String : [Can if it's possible to do new approach]
 Total Words : 8

 Given String : [I have lost        my             job]
 Total Words : 5
// Swift program
// Count number of words in a string
class MyString
{
	//Count number of words in given string
	func count_words(_ text: String) -> Int
	{
		var counter: Int = 0;
      	let str = Array(text);
		//Loop contolling variables
		var i: Int = 0;
		var j: Int = 0;
		print("\n Given String : [\(text)]");
		//Loop which is iterate given string character elements
		while (i < str.count)
		{
			if (str[i] != " ")
			{
				counter += 1;
				//loop, find word length
				j = i;
				while (j < str.count)
				{
					//Check that given character is space character or not
					if (str[j] == " ")
					{
						break;
					}
					j += 1;
				}
				//Move to current location
				i = j;
			}
			i += 1;
		}
		return counter;
	}
}
func main()
{
	let obj: MyString = MyString();
	// Simple case
	var length: Int = obj.count_words("Hello are you ready");
	print(" Total Words : ", length ,"\n", terminator: "");
	length = obj.count_words("Can if it's possible to do new approach");
	print(" Total Words : ", length ,"\n", terminator: "");
	length = obj.count_words("I have lost        my             job");
	print(" Total Words : ", length ,"\n", terminator: "");
}
main();

Output

 Given String : [Hello are you ready]
 Total Words :  4

 Given String : [Can if it's possible to do new approach]
 Total Words :  8

 Given String : [I have lost        my             job]
 Total Words :  5

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved