Strings

0 sadyemarsh22713 November 13, 2020

Strings are an array of characters i.e they are characters arranged one after another in memory. Thus, the character array is also known as a string. Each character within the string will be stored within one element of an array. A string is used by C++ to manipulate text such as words and characters.

Initializing Strings

A string is initialized as below

char name[] = {'R', 'A', 'M'};

This method is also valid but C++ provides another way to initialize string as

char name[] = "RAM";

The characters of the string are enclosed within a pair of double-quotes.

Now, We will look at an example to make clear concepts on strings.

Example:

This is a simple example where we have declared a char array.

#include <iostream>
using namespace std;
int main(){
   char chp[50] = "CHP is powered by Suresh Chand";
   cout<<chp;
   return 0;
}

The output of the above program is:

CHP is powered by Suresh Chand

Getting User input as String

Now we will learn, How we can take user input as a string. If we use cin to take input from the user then it only stores the 1st words that you passed as char and the rest words are negleted.

Let’s look a simple example

#include <iostream>
using namespace std;
int main(){
   char statement[50];
   cout<<"Enter one statement: ";
   //reading user input and store in `statement` variable
   cin>>statement;   cout<<"You entered: "<<statement;   return 0;
}

The output of the above program is:

Enter one statement: CHP is powered by Suresh Chand
You entered: CHP

Now, We will look after the correct way to get user input as string. To get full string that the user inputs, we will use cin.get function.

#include <iostream>
using namespace std;
int main(){
   char statement[50];
   cout<<"Enter one statement: ";
   //reading user input and store in `statement` variable
   cin.get(statement, 50);   cout<<"You entered: "<<statement;   return 0;
}

The output of the above program is:

Enter one statement: CHP is powered by Suresh Chand
You entered: CHP is powered by Suresh Chand

Although, this method look best to get user inputs but also it have some drawback ehich are listed below.

  • The size of memory is fixed so that we cannot allocate more memory during runtime. For example, Let’s say we have created an array of string with size 5. When user inputs 10 characters then we will only get 5 characters.  On the other  hand, If we create string with larger size then menory is wasted.
  • There are no more inbuild functions to manupulate strings.

To solve this, We will create an object of string. Let’s  look how we will do it.

String Object in C++

#include<iostream>
using namespace std;
int main(){
   // string object
   string str;
   cout<<"Enter a String:";
  
   //function to get user input
   getline(cin, str);
   cout<<"You entered: ";
   cout<<str<<endl;

   return 0;
}

The output of the above program is:

Enter a String: CHP
You entered: CHP

The advantage of this method is that we need not to declare the size of the string. This method helps us in dyanmic memory allocation so no memory will be wasted.