Skip to main content

C++ Strings

string is combination of alphabets and numbers. there are two way to create string in c++ programming. first one is very simple using char array and another one is string class.In this post explaining both of cases one by one.

Array of string

There are following way to store the string in arrays.

char name[]="Programming";
char city[]={'i','n','d','i','a','\0'};
char currency[6]={'r','u','p','e','e','\0'};
For example
#include<iostream>
using namespace std;
int main(){
  //declare arrays
  char name[]="Programming";
  char city[]={'i','n','d','i','a','\0'};
  char currency[6]={'r','u','p','e','e','\0'};
  //display values
  cout<<"name :"<<name<<endl;
  cout<<"city :"<<city<<endl;
  cout<<"currency :"<<currency<<endl;
  return 0;
}
Output
name :Programming
city :india
currency :rupee

observe that this are similar with 1D array of char. array are terminated by '\0'. This are similar to c style. normally most of case programmers are use string.

String Class in C++

string class is provide flexibility to use string in c++ programming. this are exist in string header file so when we use string class then need to include string header file. look at this example.

//Example of string class in c++
#include<iostream>
//include string header file
#include<string>
using namespace std;
int main(){
  //declaration of string
  string task="code for me";

  cout<<"string is :"<<task<<endl;
  //logically get the size
  int length=0;
  while(task[length]!='\0'){
    length++;
  }
  cout<<"length of string :"<<length<<endl;
  /*inbuilt function of string header 
    file to get number of character in string.
  */
  cout<<"length of string using length():"<<task.length()<<endl;
  cout<<"length of string using size():"<<task.size()<<endl;
  return 0;
}
Output
string is :code for me
length of string :11
length of string using length():11
length of string using size():11

In this program string is class and task is object of this class. This object are capable to store strings. And here given of three methods to get the length of object. two are inbuilt of string header file and one is logically to find null character.

Advantage of string class

Advantage of string class are can perform following operations.

Capicity:

/*Example to display Capacity in string class object*/
#include<iostream>
//include string header file
#include<string>
using namespace std;
int main(){
  //declaration of string
  string task;
  /*Capacity to store number
    of character in string class 
    Object are showing*/
  cout<<"Character Capacity : "<<task.max_size();
  return 0;
}
Output
Character Capacity : 2147483647

max_size() function are showing the capacity of characters in string object. this result can various of different c++ compiler.

Concatenation: This are a very special operation that are combined multiple group of characters to existing object. Because string object store limit are enough to deal with large group of character. and normal array limit is defined on compile time and size cannot be change. so string class are suitable in this situations. see example of combined string using (+) operator.

//Concatenation of string in c++
#include<iostream>
#include<string>
using namespace std;
int main(){
  string auxiliary="Code for";
  //concatenated
  auxiliary=auxiliary + " You ";
  cout<<auxiliary;//display
  return 0;
}
Output
Code for You

Note that in this case (+) operator are overloaded. because cannot Concat (combine) two constant string in c++ program using + operator. look at simple example and understand this point.

#include<iostream>
using namespace std;
int main(){
  //try to combine
  cout<<"da"+"bdd";
  return 0;
}
Error
error: invalid operands of types 'const char [3]' and 'const char [4]' to binary 'operator+'
  cout<<"da"+"bdd";

Functions: string class are providing various methods (functions). here given few examples.

capacity()

Capacity function are return size of string class object and includes some extra characters. see example.

#include<iostream>
#include<string>
using namespace std;
int main(){
  string team="C++ programmers team";
  //display capacity
  cout<<"Capacity :"<<team.capacity();
  return 0;
}
Output
Capacity :20
resize()

resize are used to modify size of string class object. see an example

#include<iostream>
#include<string>
using namespace std;
int main(){
  string team="C++ programmers teams, Java programmers teams";
  //resize string
  team.resize(20);
  cout<<team;//display starting 20 char.
  return 0;
}
Output
C++ programmers team




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment