Skip to main content

Split string by word

Splitting a string by space means dividing a given string into multiple substrings based on the space character (' '). For example, if we have a string "Hello world, how are you?", splitting it by space will give us the following substrings: "Hello", "world,", "how", "are", and "you?".

Here given code implementation process.

/*
    Java Program for
    Split string by word
*/
public class SplitText
{
    public static void main(String[] args)
    {
        String text = "Write a valid algorithm";
      
		// Spit text by space
        String[] words = text.split(" ");
		// Print word
        for (int i = 0; i < words.length ; ++i ) 
        {
            System.out.println(words[i]);
        }
    }
}

Output

Write
a
valid
algorithm
// Include header file
#include <iostream>
#include <string.h>

using namespace std;
/*
    C++ Program for
    Split string by word
*/
int main()
{
	char text[] = "Write a valid algorithm";
	// Spit text by space
	char *words = strtok(text, " ");
	// Print word
	while (words!=NULL)
	{
		cout << words << endl;
      	words = strtok(NULL, " ");
	}
	return 0;
}

Output

Write
a
valid
algorithm
// Include namespace system
using System;
/*
    Csharp Program for
    Split string by word
*/
public class SplitText
{
	public static void Main(String[] args)
	{
		String text = "Write a valid algorithm";
		// Spit text by space
		String[] words = text.Split(" ");
		// Print word
		for (int i = 0; i < words.Length; ++i)
		{
			Console.WriteLine(words[i]);
		}
	}
}

Output

Write
a
valid
algorithm
package main
import "strings"
import "fmt"
/*
    Go Program for
    Split string by word
*/
func main() {
	var text string = "Write a valid algorithm"
	// Spit text by space
	var words = strings.Split(text, " ")
	// Print word
	for i := 0 ; i < len(words) ; i++ {
		fmt.Println(words[i])
	}
}

Output

Write
a
valid
algorithm
<?php
/*
    Php Program for
    Split string by word
*/
function main()
{
	$text = "Write a valid algorithm";
	// Spit text by space
	$words = explode(" ", $text);
	// Print word
	for ($i = 0; $i < count($words); ++$i)
	{
		echo($words[$i]."\n");
	}
}
main();

Output

Write
a
valid
algorithm
/*
    Node JS Program for
    Split string by word
*/
function main()
{
	var text = "Write a valid algorithm";
	// Spit text by space
	var words = text.split(" ");
	// Print word
	for (var i = 0; i < words.length; ++i)
	{
		console.log(words[i]);
	}
}
main();

Output

Write
a
valid
algorithm
#    Python 3 Program for
#    Split string by word
def main() :
	text = "Write a valid algorithm"
	#  Spit text by space
	words = text.split(" ")
	i = 0
	#  Print word
	while (i < len(words)) :
		print(words[i])
		i += 1
	

if __name__ == "__main__": main()

Output

Write
a
valid
algorithm
#    Ruby Program for
#    Split string by word


def main() 
	text = "Write a valid algorithm"
	#  Spit text by space
	words = text.split(" ")
	i = 0
	#  Print word
	while (i < words.length) 
		print(words[i], "\n")
		i += 1
	end

end

main()

Output

Write
a
valid
algorithm
/*
    Scala Program for
    Split string by word
*/

object Main
{
	def main(args: Array[String]): Unit = {
		var text: String = "Write a valid algorithm";
		// Spit text by space
		var words: Array[String] = text.split(" ");
		var i: Int = 0;
		// Print word
		while (i < words.length)
		{
			println(words(i));
			i += 1;
		}
	}
}

Output

Write
a
valid
algorithm
import Foundation;
/*
    Swift 4 Program for
    Split string by word
*/

func main()
{
	let text: String = "Write a valid algorithm";
	// Spit text by space
	let words: [String] = text.split
	{
		$0 == " "
	}.map(String.init);
	var i: Int = 0;
	// Print word
	while (i < words.count)
	{
		print(words[i]);
		i += 1;
	}
}
main();

Output

Write
a
valid
algorithm
/*
    Kotlin Program for
    Split string by word
*/

fun main(args: Array < String > ): Unit
{
	val text: String = "Write a valid algorithm";
	// Spit text by space
	val words = text.split(" ");
	var i: Int = 0;
	// Print word
	while (i < words.count())
	{
		println(words[i]);
		i += 1;
	}
}

Output

Write
a
valid
algorithm




Comment

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