String Operations in C++

String Operations in C++ can be used to make the work easier by using the functions in short form of commands. And we are having different types of operations in strings. They have been listed as follows:

String Operation Functions Description
Concatenation addition operator It is used for concatenating strings together
Formatting Data reads It can read values from strings into IDL variables
Case Folding strlowcase, strupcase converts its arguments into upper case and lower case
White Space Removal STRCOMPRESS, STRTRIM Used to eliminate unwanted whitespaces from the string arguments.
Length STRLEN used for finding the length
Substrings STRPOS, STRPUT, STRMID used for inserting, locate, and extract strings from their substrings
Splitting and Joining strings STRSPLIT, STRJOIN Used to break the strings and also glues string together
Comparing strings STRCMP, STRMATCH, STREGEX It performs string comparisons

 Concatenation of Strings in C++

The Concatenation of strings in C++ helps us to join the two strings by using the ‘+’ addition operator.

Example:

string s1 = "Have a";
string s2 = "nice day";
string s3 = s1 + s2;
cout << s3 << endl;

Output:

Have a nice day

Formatting Data of strings in C++

The formatting data of strings in C++ will be having using function reads to format the data on strings. The READS procedure performs formatted input from a string variable and writes the results into one or more output variables.

It is useful when you need to examine the format of a data file before reading the information it contains. And each line of the file can be read into a string using READF. While the components of that line can be read into variables using READS.

Syntax: READS, InputVar1, …, Varn 

Case Folding in C++ | toupper, tolower

The case folding in C++ can be defined as a process applied to a sequence of characters in which those identified as a non-upper case to upper case.

Example:

#include<bits/stdc++.h> 
using namespace std; 
int main() 
{ 
   //su is the string which is converted to uppercase 
     string su = "Andhra Pradesh"; 
   //using transform() function and ::toupper in STL 
     transform(su.begin(), su.end(), su.begin(), ::toupper); 
     cout << su << endl; 
   //sl is the string which is converted to lowercase 
     string sl = "Andhra Pradesh"; 
  //using transform() function and ::tolower in STL 
     transform(sl.begin(), sl.end(), sl.begin(), ::tolower); 
     cout << sl << endl; 
     return 0; 
} 

Output:

ANDHRA PRADESH
andhra pradesh

White Space Removal in C++ | STRCOMPRESS( String, /REMOVE_ALL )

The white space removal in C++ uses compress( ) and trim( ) methods for removing unwanted spaces.

Syntax: Result = STRCOMPRESS( String, /REMOVE_ALL ) 

Example:

str = " I D L is C o o l ! "
PRINT,  str.Strlen( )
newstr = str.Compress()
; Print the new string and its length.
PRINT, newstr, newstr.Strlen( )

Output:

20
IDLisCool!
10

Length of String in C++ | strlen(string)

The strlen( ) is used for getting the length of the string.

Syntax: strlen(string)

Example:

#include<stdio.h> 
#include <string.h> 
int main() 
{ 
char ch[]={'f', 'r', 'e', 's', 'h','e','r','s','n','o','w','\0'}; 
printf("Length of string is: %d", strlen(ch)); 
return 0; 
} 

Output:

Length of string is: 11

Example 2

#include<stdio.h> 
#include<string.h> 
int main() 
{ 
char str[]= "freshers"; 
printf("Length of string is: %d", strlen(str)); 
return 0; 
} 

Output:

Length of string is: 8

Example 3

#include<stdio.h> 
#include <string.h> 
int main() 
{ 
char *str = "freshersnow"; 
printf("Length of string is: %d", strlen(str)); 
return 0; 
}

Output:

Length of string is: 11

SubStrings in C++

The Substrings in C++ function takes two values pos and len as an argument and returns a newly constructed String object with its value initialized to a copy of a substring of this object. If pos is equal to the string length, the function returns an empty string. In case if pos is greater than the string length, it throws out_of_range. If this happens, there are no changes in the string.

Syntax: 

string substr (size_t pos, size_t len) const;
Parameters:
pos: Position of the first character to be copied.
len: Length of the sub-string.
size_t: It is an unsigned integral type.
Return value: It returns a string object.

Example:

#include <string.h> 
#include <iostream> 
using namespace std; 
int main() 
{ 
   //Take any string 
    string s1 = "Freshers"; 
  //Copy three characters of s1 (starting from position 1) 
   string r = s1.substr(1, 3); 
  //prints the result 
  cout<<"String is: " << r; 
  return 0; 
} 

Output:

String is: res

Splitting and Joining Strings in C++

The splitting and joining of strings in C++ have split( ) and join( ) methods for using.

STRSPLIT()

The STRSPLIT function splits its input String argument into separate substrings, according to the specified delimiter or regular expression. And by default, the position of the substrings is returned. While the  EXTRACT keyword can be used to cause STRSPLIT to return an array containing the substrings.

Comparing Strings in C++

The comparison of strings in C++ can be done in two ways. They are as follows:

Example:

#include <iostream> 
using namespace std; 
void relationalOperation(string s1, string s2) 
{ 
      if(s1 != s2) 
       cout<< s1 << "is not equal to "<<s2 << endl; 
   if (s1 > s2) 
     cout<< s1 <<"is greater than "<< s2 << endl; 
    else
      cout<<s2 <<"is greater than"<< s1 << endl; 
} 
//Main function 
int main() 
{ 
  string s1("Freshers"); 
  string s2("freshersnow"); 
  relationalOperation(s1, s2); 
  return 0; 
}

Output:

Freshers is not equal to freshersnow
freshersnow is greater than Freshers

Example: compare( )

#include<iostream> 
using namespace std; 
void compareFunction(string s1, string s2) 
{ 
   //comparing both using inbuilt function 
     int x = s1.compare(s2); 
      if (x != 0) 
  cout<< s1 <<"is not equal to "<<s2 << endl; 
      if (x > 0) 
  cout<< s1 <<"is greater than"<<s2 << endl; 
      else
  cout<< s2 <<"is greater than "<<s1 << endl; 
} 
//Main function 
int main() 
{ 
  string s1("Freshers"); 
  string s2("freshersnow"); 
  compareFunction(s1, s2); 
  return 0; 
} 

Output:

Freshers is not equal to freshersnow
freshersnow is greater than Freshers