C++ String


The string is an array of characters that is terminated by the null character ( \0 ).

There are two ways to declare the string.

  • char array
  • string literal

String by char array:

char ch[5]={‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};

String by string literal:

 char ch[ ]=”hello”;

Here \0 would automatically insert at the end in this type of declaration.


Example:

#include <iostream>   
#include <cstring>  
using namespace std;
   
int main(){    
    char a[14]={'h','e','l','l','o','\0'};    
    char b[]="onlinetpoint";    
    cout << "Char Array Value for a is: " << a << endl;    
    cout << "String Literal Value For b is: " << b;  
    return 0;  
} 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.