Advertisement
// First approach to create a string in C++
#include<iostream>
int main()
{
char arr[] = {'W', 'e', 'l', 'c', 'o', 'm', 'e', '\0'};
}
// Another approach to create a string in C++
#include<iostream>
int main()
{
char arr[] = "Welcome"; // no need to end this string with \0
}
Advertisement
// Creating multi-word strings in C++
#include<iostream>
int main()
{
/* creating a multi-word string - Hello there */
char arr1[] = "Hello there";
/* creating a multi-word string - How are you? */
char arr2[] = "How are you?";
/* creating a multi-word string - Hi there */
char arr3[] = {'H', 'i', ' ', 't', 'h', 'e', 'r', 'e'. '\0'};
return 0;
}
// Using for loop to print a string a string in C++
#include<iostream>
using namespace std;
int main()
{
// Creating a multi-word string i.e. Hello World
char arr[] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0'};
for(int i=0;i<11; i++)
{
cout<<arr[i]; // printing each character in string/char array using index
}
return 0;
}
Hello World
/* Using while loop to print a string a string in C++ */
#include<iostream>
using namespace std;
int main()
{
/* Creating a multi-word string i.e. Hello World */
char arr[] = "Hello World";
int i=0;
while(i<11)
{
cout<<arr[i]; // printing each character in string/char array using index
i++;
}
return 0;
}
Hello World
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement