String in C++
String is a collection of characters. In C++ there are two types of strings commonly used:-
- The C-style strings.
- The string class type (The Standard C++ Library string class).
The C-style String:
- In C programming, A string is an array of characters which is terminated by null character ( '\0' ), is also supported in C++ programming language.
- Each character in array occupies one byte of memory and the last character is always null character ( '\0' ) {backslash zero}.
- for e.g:
char s [6] = {'H','E','L','L','O','\0'}; OR char s [ ] = {'H','E','L','L','O','\0'}; OR char s [6] = "HELLO"; OR char s [ ] = "HELLO";
The above string can be represented as:
- NOTE:
If you initialize a string by specifying character by character at every index in array (as in first example) then you must have to put null ( '\0' ) character at the end yourself.
But, if you initialize a string by string literal (providing group of characters between " ") then null character ( '\0' ) is automatically placed by C++ compiler at the end. - Therefore, the size of string must be equal to the maximum number of characters+1 .
Example:
1. Program store name into string and display.#include<iostream> using namespace std; int main(){ char name[15] = "prowessapps.in"; cout<< name; return 0; }OUTPUT:
prowessapps.in2. Program to display a string entered by user..
#include<iostream> using namespace std; int main() { char s[50]; cout<< "Enter a string:"; cin>>s; cout<< "**************\n"; cout<< "Entered string is:\n"; cout<< s<< endl; cout<< "**************"; return 0; }OUTPUT:
FIRST RUN: Enter a string:prowessapps.in ************** Entered string is: prowessapps.in ************** ------------------------------------- SECOND RUN: Enter a string:C++ is a programming language ************** Entered string is: C++ **************
As you can notice that, in the second run only "C++" is displayed instead of "C++ is a programming language".
This is because the extraction operator >> works as scanf() in 'C' and considers a space " " is a string terminating character.
Example:
C++ String to read a line of text3. Program to read and display an entire line entered by user..
#include<iostream> using namespace std; int main() { char s[50]; cout<< "Enter a string:"; cin.get(s,50); cout<< "**************\n"; cout<< "Entered string is:\n"; cout<< s<< endl; cout<< "**************"; return 0; }OUTPUT:
Enter a string:C++ is a programming language ************** Entered string is: C++ is a programming language **************
To read the string containing blank space, use cin.get()
function. This function takes two arguments.
First argument is the name of the string (address of first element of string) and
second argument is the maximum size of the array.
C++ library supports a wide range of string handling functions, <cstring>
is a standard header file that is use to accessing the string handling functions.
S.No | Function Name | Purpose |
---|---|---|
1. | strcpy(str1, str2) | The strcpy function copies string2 into string1. |
2. | strcat(str1, str2) | The strcat function joins two string together.It copies string2 to string1. |
3. | strlen(str1) | Returns the length of string str1. |
4. | strcmp(str1, str2) | Returns 0 if str1 and str2 are the same; less than 0 if str1<s2; greater than 0 if s1>s2. |
5. | strstr(str1, str2) | Returns a pointer to the first occurrence of string2 in string s1. |
Example:
3. Program to makes use of the above-mentioned functions:.#include<iostream> #include<cstring> using namespace std; int main() { char str1[10]="Hello"; char str2[10]=" World"; char str3[20]; strcpy(str3,str1); //str3 will be " World" cout<< "strcpy(str3,str1) = "; cout<< str3<< endl; int l = strlen(str1); cout<< "strlen(str1) = "; cout<< l<< endl; strcat(str1,str2); //str1 will be "Hello World" cout<<" strcat(str1,str2) = "; cout<< str1<< endl; int c = strcmp(str1,str2); //c will be 1(one) cout<<" strcmp(str1,str2) = "; cout<< c<< endl; return 0; }OUTPUT:
strcpy(str3,str1) = Hello strlen(str1) = 5 strcat(str1,str2) = Hello World strcmp(str1,str2) = 1
The String Class in C++:
- Strings are objects that represent sequences of characters.
- Unlike using char arrays, string objects has no fixed length, and can be extended as per your requirement.
- The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality.
Example:
#include<iostream> #include<cstring> using namespace std; int main() { string str1 ="Hello"; string str2 =" World"; string str3; //copy str1 into str3 str3 = str1; //str3 will be " World" cout<<"str3 = "<< str3<< endl; //find the length of str1 int l = str1.size(); cout<<"length of str1 = "<< l<< endl; //concatenates str1 and str2 str3 = str1 + str2; cout<<"str3 = "<< str3<< endl; return 0; }
Example:
WAP to take and display the user's name including space.#include<iostream> using namespace std; int main() { string name; cout<<"Enter Name : "; getline(cin,name); cout<<"Entered Name is :"; cout<< name; return 0; }OUTPUT:
Enter Name : Ayan Khan Entered Name is :Ayan Khan
Next topic is Stream Based I/O
Online Live Training
We provide online live training on a wide range of technologies for working professionals from Corporate. We also provide training for students from all streams such as Computer Science, Information Technology, Electrical and Mechanical Engineering, MCA, BCA.
Courses Offered :
- C Programming
- C++ Programming
- Data Structure
- Core Java
- Python
- Java Script
- Advance Java (J2EE)
- Hibernate
- Spring
- Spring Boot
- Data Science
- JUnit
- TestNg
- Git
- Maven
- Automation Testing - Selenium
- API Testing
NOTE: The training is delivered in full during weekends and during the evenings during the week, depending on the schedule.
If you have any requirements, please send them to prowessapps.in@gmail.com or info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in