String in C++ can be represented in two ways:
- C style strings
It involves using an array of characters. Most of the c style function rely on the fact that all of the string must be ‘\0’ terminated. For this reason, the character arrays are declared one character longer than the largest string they can hold. The end of the string is determined by checking for null character.
Let us see how to declare a c style string
char str [] = “string”;
#include<bits/stdc++.h>
using namespace std;
int main()
{
char str [] = "Hey";
cout<<str<<" ";
cout<<sizeof(str); //returns 4
return 0;
}
The compiler automatically inserts a null character at the end of a c style string.
Functions used for C style string:
strlen(str) – It returns the length of string .
strcmp(s1, s2) – It compares the two strings lexicographically. If the first string is greater than the second string, it returns positive value, if both strings are same it returns 0 and if the first string is smaller than the second it returns a negative value. Let’s see an example of strcmp.
#include<bits/stdc++.h>
using namespace std;
int main()
{
char s1 [] = "abc";
char s2 [] = "bcd";
int res = strcmp(s1, s2);
if(res>0){
cout<<"Greater"<<endl;
}
else if(res == 0){
cout<<"Same"<<endl;
}
else{
cout<<"Smaller"<<endl;
}
return 0;
}
The above code prints smaller since “abc” is lexicographically smaller than “bcd”.
strcpy(s1, s2) – it creates a copy of s2 and stores it in s1.
#include<bits/stdc++.h>
using namespace std;
int main()
{
char str [4];
strcpy(str, "HTH");
cout<<str<<endl;
return 0;
}
- C++ style string
It is strongly recommend to use c++ strings over c style strings. This is because c++ style strings have richer library. Moreover, they also support operations like +, <,>, <=, >=
Syntax for declaring c++ string is:
string str=”stringname”;
Now let us see how and perform some operations in a c++ style string.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str = "good morning";
cout<<str.length()<<endl;
str = str + "xyz"; //Concatenates two strings
cout<<str<<endl; //Prints good morningxyz
cout<<str.substr(1, 3)<<endl; //Prints ood
cout<<str.find("ood")<<endl; //Prints 1 as ood is present in the string
return 0;
}
str.substr function takes two parameter: starting index and length of substring and then returns the corresponding substring.
str.find function takes a substring as a parameter and returns the index of first occurrence of the given substring. If it is not found, a special constant is returned which is string::npos.
Let us now see how relational operator can be used in strings.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1 = "abc";
string s2 = "bcd";
if(s1>s2){
cout<<"Greater"<<endl;
}
else if(s1 == s2){
cout<<"Same"<<endl;
}
else{
cout<<"Smaller"<<endl;
}
return 0;
}
Reading string from console
The getline function is used to read the string from console.
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
string str;
getline(cin, str);
cout<<str<<endl;
return 0;
}
getline function has an optional third argument that specifies when to stop reading the string. By default, it’s value is a newline character . For eg : getline(cin,str,’a’) tells the compiler to stop reading the string when a is encountered.
Iterating through strings-
We can access the elements of the string via index or using a range based loop.
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
string str = "HackTechHub";
//Using Index Based Loop
for(int i = 0; i<str.length(); i++)
cout<<str[i];
cout<<"\n";
//Using Range Based Loop
for(char &x: str)
cout<<x;
return 0;
}
This Article Was Contributed By Ritika Semwal