C++ cstring to string

·

3 min read

  • Cstring is array which has base type char and its ending marked with null, ‘\0’.

  • C++ strings is Objects of string class

Image description

C++ Strings

String is one of the most useful data types supplied C++ libraries is the string. String is a declared variable that stores a sequence of letters and other characters. To create a string first declare, then store a value in it.

string kodlogs;

kodlogs = "This love Kodlogs.";

Or we can combine these two statements into one line:

string kodlogs = " This love Kodlogs.";

C string

C string is an array of characters end with a special character. This special character is called the NULL character, and this NULL character is called a NULL byte. The binary value of NULL character is 0. NULL character can be written as '\0' in a program. NULL character block one character position in the array. C string that can store X characters must be declared to have length X+1. Examples

char kodlogs[11]; // can store up to 10 characters, no more char

C string can be declared by as:

char numberone[11] = “Kodlogs”; // number of characters in the string literal must be at most one less than this length.
char numbertwo[] = “Kodloger”; // compiler determines how large to make the array of characters.
char* numberthree= “You will love Kodlogs”; // variable numberthree is a pointer

Unlike C++ strings, C strings are fixed size. They don’t increase automatically while adding more characters. They cannot be resized. cstrings are just arrays of characters.

Declaring c-strings don’t require a library, but manipulations require library

cstring to string

There are 4 easy methods to convert cstring to sting.

  • By using string constructer ``` #include

using namespace std;

int main()

{

const char* kodlogs = "Kodlogs is the best!"; // C-style string

string abc(kodlogs);// string constructor use const char* as a parameter

cout << abc <

return 0;

}

- **By using string.append function**

#include

using namespace std;

int main()

{

const char* kodlogs = "Kodlogs is the best!";// C-style string

string abc;

abc.append(kodlogs);

cout << abc <

return 0;

}

- **By using + operator**

#include

using namespace std;

int main()

{

const char* kodlogs = "Kodlogs is the best!";// C-style string

string abc;

abc += kodlogs;

cout << abc <

return 0;

}

- **By using string.assign function**

#include

using namespace std;

int main()

{

const char* kodlogs = "Kodlogs is the best!";// C-style string

string abc;

abc.assign(kodlogs);

cout << abc <

return 0;

} ```

Use of changing cstring to string

  • unlike C strings, String manages its own space.

  • While using string ‘+’ operator can be used for concatenation, ‘=’ used for assignment, and string can be compared using regular operators.

  • Unlike C string many functions like find() can be implemented.

Summary

String is one of the most useful data types supplied C++ libraries is the string. C string is an array of characters end with a special character. This special character is called the NULL character, and this NULL character is called a NULL byte. To convert cstring to sting we can use string constructer, string.append function, string.assign function and + operator.