C++ String | string object | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Tuesday, July 21, 2020

C++ String | string object | My CS Tutorial

C++ string | initialization of string | string object in c++ | example | My CS Tutorial

String in C++

String is nothing but the collection of the individual array elements or characters stored at contiguous memory locations. String is enclosed within Double quotes.

 String always Terminated with NULL Character (‘/0′).

Syntax:

Char Stringname[sizeofcharacters];

Example : char name[10];

Initializing String [Character Array] :

Whenever we declare a String then it will contain garbage values inside it.
We have to initialize String or Character array before using it. Process of assigning some legal default data to String is Called Initialization of String.

char name [ ] = {'H','A','R','S','H','\0'};

In the above example name is assigned with a string HARSH and Ends with Null
Character.

C++ string | My CS Tutorial

Strings are words that are made up of characters, hence they are known as sequence of characters. In C++ we have two ways to create and use strings: 1) By creating char arrays and treat them as string 2) By creating string object

Lets discuss these two ways of creating string first and then we will see which method is better and why.

1) Array of Characters – Also known as C Strings

Example 1:

A simple example where we have initialized the char array during declaration.

#include <iostream>
using namespace std;
int main(){
   char book[50] = "A Song of Ice and Fire";
   cout<<book;
   return 0;
}

Output:

A Song of Ice and Fire

Example 2: Getting user input as string

This can be considered as inefficient method of reading user input, why? Because when we read the user input string using cin then only the first word of the string is stored in char array and rest get ignored. The cin function considers the space in the string as delimiter and ignore the part after it.

#include <iostream>
using namespace std;
int main(){
   char book[50];
   cout<<"Enter your favorite book name:";
   //reading user input
   cin>>book;
   cout<<"You entered: "<<book;
   return 0;
}

Output:

Enter your favorite book name:The Murder of Roger Ackroyd
You entered: The

You can see that only the “The” got captured in the book and remaining part after space got ignored. How to deal with this then? Well, for this we can use cin.get function, which reads the complete line entered by user.

String function

strcpy(s1, s2);

Copies string s2 into string s1

strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

strlen(s1);

Returns the length of string s1.

strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.

strchr(s1, ch);

Returns a pointer to the first occurrence of character ch in string s1.

strstr(s1, s2);


Returns a pointer to the first occurrence of string s2 in string s1.

Example 3: Correct way of capturing user input string using cin.get

#include <iostream>
using namespace std;
int main(){
   char book[50];
   cout<<"Enter your favorite book name:";

   //reading user input
   cin.get(book, 50);
   cout<<"You entered: "<<book;
   return 0;
}
Output:

Enter your favorite book name:The Murder of Roger Ackroyd
You entered: The Murder of Roger Ackroyd


Drawback of this method


1) Size of the char array is fixed, which means the size of the string created through it is fixed in size, more memory cannot be allocated to it during runtime. For example, lets say you have created an array of character with the size 10 and user enters the string of size 15 then the last five characters would be truncated from the string.
On the other hand if you create a larger array to accommodate user input then the memory is wasted if the user input is small and array is much larger then what is needed.

2) In this method, you can only use the in-built functions created for array which don’t help much in string manipulation.

What is the solution of these problems?

We can create string using string object. Lets see how we can do it.

String object in C++


Till now we have seen how to handle strings in C++ using char arrays. Lets see another and better way of handling strings in C++ – string objects.

#include<iostream>
using namespace std;
int main(){
   // This is how we create string object
   string str;
   cout<<"Enter a String:";
   /* This is used to get the user input
    * and store it into str
    */
   getline(cin,str);
   cout<<"You entered: ";
   cout<<str<<endl;

   /* This function adds a character at
    * the end of the string
    */ str.push_back('A');
   cout<<"The string after push_back: "<<str<<endl;
   /* This function deletes a character from
    * the end of the string
    */
   str.pop_back();
   cout << "The string after pop_back: "<<str<<endl;
   return 0;
}

Output:

Enter a String:XYZ
You entered: XYZ
The string after push_back: XYZA
The string after pop_back: XYZ

The advantage of using this method is that you need not to declare the size of the string, the size is determined at run time, so this is better memory management method. The memory is allocated dynamically at runtime so no memory is wasted.

C++ string | initialization of string | string object in c++ | example | My CS Tutorial

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment