Strings in C++

Strings are character arrays whose last value is '\0', null character.

You can define a string as :

 char str[] = {'h', 'e', 'y', '\0' }  
or as 
 char str[] = "hey";  // str can be modified 
or as
 char * str = "hey"; // str cannot be modified, will cause segmentation error
 const char * str2 = "heyy"; // will not cause warning, still cannot modify
or by using the string data type
 string s = "hey";  

The string class, included as #include <string> provides various functions to perform string operations. Some of them are :


strlen(str) -> length of str


strcat(str1,str2) -> concatenate str2 at the end of str1


strcpy(str1,str2) -> copy str2 into str1


When you declare as char[int] then you cannot directly assign value using = rather you need to use strcpy

Comments

Popular posts from this blog

In Place Algorithms

References in C++