Skip to main content

Convert char array to string

A char array is a sequence of characters stored in consecutive memory locations in an array. On the other hand, a string is a sequence of characters that are terminated by a null character '\0'. To convert a char array to a string means to create a string that contains the same sequence of characters as the original char array.

Program Solution

// Include header file
#include <iostream>

#include <string.h>

using namespace std;
/*
    C++ program for
    Convert char array to string
*/

int main()
{
	char text[] = {
		'E' , 'x' , 'a' , 'm' , 'p' , 'l' , 'e'
	};
	// Example A
	cout << string(text) << endl;
	string ans = "";
	// Append text characters into ans    
	for (int i = 0; i < sizeof(text) / sizeof(text[0]); ++i)
	{
		ans += text[i];
	}
	cout << ans << endl;
	return 0;
}

Output

Example
Example
/*
    Java program for
    Convert char array to string
*/
public class Conversion
{
	public static void main(String[] args)
	{
		char[] text = {
			'E' , 'x' , 'a', 'm' , 'p' , 'l' , 'e'
		};
		// Example A
		System.out.println(String.valueOf(text));
		// Example B
		System.out.println(new String(text));
		// Example C
		String ans = "";
		// Append text characters into ans    
		for (int i = 0; i < text.length; ++i)
		{
			ans += text[i];
		}
		System.out.println(ans);
	}
}

Output

Example
Example
Example
// Include namespace system
using System;
/*
    Csharp program for
    Convert char array to string
*/
public class Conversion
{
	public static void Main(String[] args)
	{
		char[] text = {
			'E' , 'x' , 'a' , 'm' , 'p' , 'l' , 'e'
		};
		// Example A
		Console.WriteLine(string.Join("",text));
		// Example B
		Console.WriteLine(new String(text));
		// Example C
		String ans = "";
		// Append text characters into ans    
		for (int i = 0; i < text.Length; ++i)
		{
			ans += text[i];
		}
		Console.WriteLine(ans);
	}
}

Output

Example
Example
Example
<?php
/*
    Php program for
    Convert char array to string
*/
function main()
{
	$text = array('E', 'x', 'a', 'm', 'p', 'l', 'e');
	// Example A
	echo(implode("", $text)."\n");
	// Example B
	$ans = "";
	// Append text characters into ans    
	for ($i = 0; $i < count($text); ++$i)
	{
		$ans .= $text[$i];
	}
	echo($ans."\n");
}
main();

Output

Example
Example
/*
    Node JS program for
    Convert char array to string
*/
class Conversion
{}

function main()
{
	var text = ['E', 'x', 'a', 'm', 'p', 'l', 'e'];
	// Example A
	console.log(text.join(""));
	// Example B
	var ans = "";
	// Append text characters into ans    
	for (var i = 0; i < text.length; ++i)
	{
		ans += text[i];
	}
	console.log(ans);
}
main();

Output

Example
Example
#    Python 3 program for
#    Convert char array to string
def main() :
	text = ['E', 'x', 'a', 'm', 'p', 'l', 'e']
	#  Example A
	print("".join(text))
	#  Example B
	ans = ""
	i = 0
	#  Append text characters into ans    
	while (i < len(text)) :
		ans += text[i]
		i += 1
	
	print(ans)

if __name__ == "__main__": main()

Output

Example
Example
/*
    Scala program for
    Convert char array to string
*/
class Conversion()
{}
object Main
{
	def main(args: Array[String]): Unit = {
		var text: Array[Char] = Array('E', 'x', 'a', 'm', 'p', 'l', 'e');
		// Example A
		println(String.valueOf(text));
		// Example B
		println(new String(text));
		// Example C
		var ans: String = "";
		var i: Int = 0;
		// Append text characters into ans    
		while (i < text.length)
		{
			ans += text(i);
			i += 1;
		}
		println(ans);
	}
}

Output

Example
Example
Example
import Foundation;
/*
    Swift 4 program for
    Convert char array to string
*/
func main()
{
	let text: [Character] = ["E", "x", "a", "m", "p", "l", "e"];
	// Example A
	print(String(text));
	// Example C
	var ans: String = "";
	var i: Int = 0;
	// Append text characters into ans    
	while (i < text.count)
	{
		ans += String(text[i]);
		i += 1;
	}
	print(ans);
}
main();

Output

Example
Example
/*
    Kotlin program for
    Convert char array to string
*/
class Conversion
{}
fun main(args: Array < String > ): Unit
{
	val text: Array < Char > = arrayOf('E', 'x', 'a', 'm', 'p', 'l', 'e');
	// Example A
	println(text.joinToString(""));
	// Example B
	var ans: String = "";
	var i: Int = 0;
	// Append text characters into ans    
	while (i < text.count())
	{
		ans += text[i];
		i += 1;
	}
	println(ans);
}

Output

Example
Example




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