Posted on by Kalkicode
Code Conversion

Convert string to char array

In programming, a string is a sequence of characters that is represented as a single data type. A char array, on the other hand, is an array of characters where each element in the array is a single character.

To convert a string to a char array, you need to break down the string into its individual characters and store them in an array of characters. This process is known as "string to char array conversion".

Program Solution

/*
    Java program for
    Convert string to char array
*/
public class Conversion
{
	public void printArray(char[] ans, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + ans[i]);
		}
		System.out.print("\n");
	}
	public static void main(String[] args)
	{
		Conversion task = new Conversion();
		// Given text
		String str1 = "abcde";
		String str2 = "1b2c3d4e";
		// Get the length of string
		int n1 = str1.length();
		int n2 = str2.length();
		// Case A
		char[] ans1 = str1.toCharArray();
		// Case B
		char[] ans2 = new char[n2];
		for (int i = 0; i < n2; ++i)
		{
			ans2[i] = str2.charAt(i);
		}
		// Display result
		task.printArray(ans1, n1);
		task.printArray(ans2, n2);
	}
}

Output

 a b c d e
 1 b 2 c 3 d 4 e
// Include header file
#include <iostream> 
#include <string.h>

using namespace std;
/*
    C++ program for
    Convert string to char array
*/
class Conversion
{
	public: void printArray(char ans[], int n)
	{
		for (int i = 0; i < n; ++i)
		{
			cout << " " << ans[i];
		}
		cout << "\n";
	}
};
int main()
{
	Conversion *task = new Conversion();
	// Given text
	string str1 = "abcde";
	string str2 = "1b2c3d4e";
	// Get the length of string
	int n1 = str1.length();
	int n2 = str2.length();
	// Case A
  	char ans1[n1];
	strcpy(ans1, str1.c_str());
	// Case B
	char ans2[n2];
	for (int i = 0; i < n2; ++i)
	{
		ans2[i] = str2[i];
	}
	// Display result
	task->printArray(ans1, n1);
	task->printArray(ans2, n2);
	return 0;
}

Output

 a b c d e
 1 b 2 c 3 d 4 e
// Include namespace system
using System;
/*
    Csharp program for
    Convert string to char array
*/
public class Conversion
{
	public void printArray(char[] ans, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + ans[i]);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		Conversion task = new Conversion();
		// Given text
		String str1 = "abcde";
		String str2 = "1b2c3d4e";
		// Get the length of string
		int n1 = str1.Length;
		int n2 = str2.Length;
		// Case A
		char[] ans1 = str1.ToCharArray();
		// Case B
		char[] ans2 = new char[n2];
		for (int i = 0; i < n2; ++i)
		{
			ans2[i] = str2[i];
		}
		// Display result
		task.printArray(ans1, n1);
		task.printArray(ans2, n2);
	}
}

Output

 a b c d e
 1 b 2 c 3 d 4 e
package main

import "fmt"
/*
    Go program for
    Convert string to char array
*/

func printArray(ans[] byte, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Printf(" %c", ans[i])
	}
	fmt.Print("\n")
}
func main() {

	// Given text
	var str1 string = "abcde"
	var str2 string = "1b2c3d4e"
	// Get the length of string
	var n2 int = len(str2)
	// Case A
	var ans1 = []rune(str1)
	// Case B
	var ans2 = make([] byte,n2)
	for i := 0 ; i < n2 ; i++ {
		ans2[i] = str2[i]
	}
	// Display result
	fmt.Printf("%c\n",ans1)
	printArray(ans2, n2)
}

Output

[a b c d e]
 1 b 2 c 3 d 4 e
<?php
/*
    Php program for
    Convert string to char array
*/
class Conversion
{
	public	function printArray($ans, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$ans[$i]);
		}
		echo("\n");
	}
}

function main()
{
	$task = new Conversion();
	// Given text
	$str1 = "abcde";
	$str2 = "1b2c3d4e";
	// Get the length of string
	$n1 = strlen($str1);
	$n2 = strlen($str2);
	// Case A
	$ans1 = str_split($str1);
	// Case B
	$ans2 = array_fill(0, $n2, ' ');
	for ($i = 0; $i < $n2; ++$i)
	{
		$ans2[$i] = $str2[$i];
	}
	// Display result
	$task->printArray($ans1, $n1);
	$task->printArray($ans2, $n2);
}
main();

Output

 a b c d e
 1 b 2 c 3 d 4 e
/*
    Node JS program for
    Convert string to char array
*/
class Conversion
{
	printArray(ans, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + ans[i]);
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new Conversion();
	// Given text
	var str1 = "abcde";
	var str2 = "1b2c3d4e";
	// Get the length of string
	var n1 = str1.length;
	var n2 = str2.length;
	// Case A
	var ans1 = (str1).split('');
	// Case B
	var ans2 = Array(n2).fill(' ');
	for (var i = 0; i < n2; ++i)
	{
		ans2[i] = str2.charAt(i);
	}
	// Display result
	task.printArray(ans1, n1);
	task.printArray(ans2, n2);
}
main();

Output

 a b c d e
 1 b 2 c 3 d 4 e
#    Python 3 program for
#    Convert string to char array
class Conversion :
	def printArray(self, ans, n) :
		i = 0
		while (i < n) :
			print(" ", ans[i], end = "")
			i += 1
		
		print(end = "\n")
	

def main() :
	task = Conversion()
	#  Given text
	str1 = "abcde"
	str2 = "1b2c3d4e"
	#  Get the length of string
	n1 = len(str1)
	n2 = len(str2)
	#  Case A
	ans1 = list(str1)
	#  Case B
	ans2 = [ ' ' ] * (n2)
	i = 0
	while (i < n2) :
		ans2[i] = str2[i]
		i += 1
	
	#  Display result
	task.printArray(ans1, n1)
	task.printArray(ans2, n2)

if __name__ == "__main__": main()

Output

  a  b  c  d  e
  1  b  2  c  3  d  4  e
#    Ruby program for
#    Convert string to char array
class Conversion 
	def printArray(ans, n) 
		i = 0
		while (i < n) 
			print(" ", ans[i])
			i += 1
		end

		print("\n")
	end

end

def main() 
	task = Conversion.new()
	#  Given text
	str1 = "abcde"
	str2 = "1b2c3d4e"
	#  Get the length of string
	n1 = str1.length
	n2 = str2.length
	#  Case A
	ans1 = str1.chars
	#  Case B
	ans2 = Array.new(n2) { ' '
	}
	i = 0
	while (i < n2) 
		ans2[i] = str2[i]
		i += 1
	end

	#  Display result
	task.printArray(ans1, n1)
	task.printArray(ans2, n2)
end

main()

Output

 a b c d e
 1 b 2 c 3 d 4 e
import scala.collection.mutable._;
/*
    Scala program for
    Convert string to char array
*/
class Conversion()
{
	def printArray(ans: Array[Char], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + ans(i));
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Conversion = new Conversion();
		// Given text
		var str1: String = "abcde";
		var str2: String = "1b2c3d4e";
		// Get the length of string
		var n1: Int = str1.length();
		var n2: Int = str2.length();
		// Case A
		var ans1: Array[Char] = str1.toCharArray();
		// Case B
		var ans2: Array[Char] = Array.fill[Char](n2)(' ');
		var i: Int = 0;
		while (i < n2)
		{
			ans2(i) = str2.charAt(i);
			i += 1;
		}
		// Display result
		task.printArray(ans1, n1);
		task.printArray(ans2, n2);
	}
}

Output

 a b c d e
 1 b 2 c 3 d 4 e
import Foundation;
/*
    Swift 4 program for
    Convert string to char array
*/
class Conversion
{
	func printArray(_ ans: [Character], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", ans[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let task: Conversion = Conversion();
	// Given text
	let str: String = "abcde";
	// Get the length of string
	let n: Int = str.count;
	let ans: [Character] = Array(str);
	// Display result
	task.printArray(ans, n);

}
main();

Output

  a  b  c  d  e
/*
    Kotlin program for
    Convert string to char array
*/
class Conversion
{
	fun printArray(ans: Array < Char > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + ans[i]);
			i += 1;
		}
		print("\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Conversion = Conversion();
	// Given text
	val str1: String = "abcde";
	val str2: String = "1b2c3d4e";
	// Get the length of string
	val n1: Int = str1.length;
	val n2: Int = str2.length;
	// Case A
	var ans1: Array < Char > = str1.toCharArray().toTypedArray();
	// Case B
	var ans2: Array < Char > = Array(n2)
	{
		' '
	};
	var i: Int = 0;
	while (i < n2)
	{
		ans2[i] = str2.get(i);
		i += 1;
	}
	// Display result
	task.printArray(ans1, n1);
	task.printArray(ans2, n2);
}

Output

 a b c d e
 1 b 2 c 3 d 4 e

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