c++ string

C++ string

In this post, you will learn about the declaration, syntax, and how to use the string in the C++ language.

The collection of the characters is called the string
In the CPP, two types of string mostly used.
  1. The C-style character string.
  2. The string object in the standard library of the CPP.

What is c-style string?

C-style string

The c-style string is the drive from the C language. Actually,CPP created after the C language is the mother language of the CPP, and a lot of the properties of the C language are found in the C++ language.

The C-style string based upon the array of the characters with NULL character as a terminated character at the end of the array. The array is a one-dimensional array.

How to declare C-style string?

Char str[7]={'s', 't', 'r', 'i', 'n', 'g', '\0'};

In the above declaration, NULL character is in the end of the string which tells the compiler this end of the string.

Other way of the declaring the string

char str[]="string";

In this way, the compiler automatically placed the NULL-terminated character at the end of the string because does not write during the declaration of the string.

Example of the C-String style:

#include<iostream>
using namespace std;
int main()
{
//use the C-style string in this program
    char str[7]={'s', 't', 'r', 'i', 'n', 'g', '\0'};
cout<<str<<endl;
//another way to defining the string
char ch[]="string";
cout<<ch;
return 0;
}

Output:
string
string

c string

Take the input string from the user:

The taking input string from the user a little bit confusing in the beginning. There are two ways to take input.
Take simple input
#include<iostream>
using namespace std;
int main()
{
char ch[100];
cout<<"enter the string ";
cin>>ch;
cout<<"output:"<<ch;
return 0;
}

Output
enter the string cpp program
output: cpp

In the above code, Notice that output string skip program instead of print cpp program.This is due to the extraction(>>) operator take space as the null character and skip the rest of the string.

Taking input through cin.getline

The above problem solves by the use of the cin.getline during taking input.
#include<iostream>
using namespace std;
int main()
{
char ch[100];
cout<<"enter the string ";
cin.get(ch,100);
cout<<"output:"<<ch;
return 0;
}

Output
enter the string cpp program
output:cpp program

The cin.get function takes two arguments,one is the name of the function and second is the size of the string.cin.get ignore the spaces in the string.

What is string object in c++?

 C++ string object:

In the CPP language, allow making the object of the string. By making the string object, the number one benefit you can increase as requirements.

#include<iostream>
using namespace std;
int main()
{
string str;
cout<<"enter the string ";
getline(cin,str);
cout<<"output :"<<str;
return 0;
}

Output
enter the string This is beautiful
output :This is beautiful


In this above code,getline function is used for taking string input.The important thing is to be noted is that object of the string is used as str . The object str of the class string which is available in the CPP langauge.

Please write the comment,if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

Flutter layout

Diferrence between flutter and react native

how to cout in cpp