How to Declare & Initialise a String in C++
Steps:
- Include the necessary header files ( #include<string> ).
- Declare a string variable using the
std::string
type. - Initialize the string variable with the desired value.
For example:
C++
#include <iostream> #include <string> int main() { // Declare and initialize a string std::string myString = "Hello, World!" ; // Print the string std::cout << myString << std::endl; return 0; } |
Javascript
// Declare and initialize a string const GFG = "Hello, World!" ; // Print the string console.log(GFG); |
Output
Hello, World!
How to Declare & Initialise a String in different Programming languages?
To declare and initialize a string in different programming languages, you can use language-specific syntax and functions. Below are examples in C++, C#, Python, JavaScript, and Java, with explanations for each language: