C++

C++ Strings – Detailed Instructions From [A-Z]

Strings are character-based words, which is why they are often referred to as character sequences. There are two methods in C++ for creating and using strings: 1) By making string-like char arrays. 2) Using a string object

Before determining which approach is superior and why, let’s first talk about these two methods for producing strings.

1) Character Array, often called C Strings

Example 1: 

A straightforward illustration where the char array was initialized 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

Why is this a method of reading user input that can be deemed inefficient? Because only the first word of the user-input string is stored in the char array when we read it using cin, and the rest is ignored. The cin function ignores the text after the space in the string and treats it as a delimiter.

#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 the book simply included the word “The” and ignored everything else after that. Then, how do we handle this? To do this, we can use the cin.get method, which reads the entire line that the user has entered.

Example 3: Using cin.get correctly to capture user input string

#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

Cons of this approach

1) The size of the char array is fixed, therefore more memory cannot be allocated to it during runtime, and as a result, the size of the string formed by it is also fixed. For instance, if a user inserts a string of size 15 into a character array you’ve generated with a size of 10, the final five characters in the string will be omitted.
However, if the user input is modest and the array is considerably larger than what is required, creating a larger array to accommodate user input wastes memory.

2) The only built-in functions for arrays that are useful for manipulating strings can be used in this technique.

What is the answer to these issues?
By using the string object, we can build strings. Let’s investigate our options.

C++’s String object

Up until now, char arrays have been used to handle strings in C++. Let’s examine a different and superior method for managing 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 benefit of utilizing this method is that the string’s size is determined at runtime rather than in advance, making it a better memory management technique. No memory is lost because it is dynamically allocated at runtime.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button