C++ Tutorials

C++ Tutorials

Substring c++

How to find lexicographical concatenation of all substrings of a string Let’s have a look at the problem statement. Given a string, find concatenation of all substrings in lexicographic order.

For input string “abc”, Output will be aababcbbcc. Concatenation of substrings in lexicographic order is “a”+”ab”+”abc”+”b”+”bc”+”c” = “aababcbbcc”.

Now let’s look at the solution.

First. Find all the substrings of string and store them in a string array. The size of array would be n*(n+1)/2 where n is the length of input string.

Second. Sort the string array to make them all in lexicographical order.

Third. Concatenate the strings of string array in another empty string. Now let’s look at the implementation.

First, we Find all the substrings of string and store it in an array arr. We sue a double loop to do this. I goes from 0 to n and len goes from 1 to n-1. We find substring using s.substr(i, len) which returns substring from index i and length len and add it to the array.

Now, Sort the string array to make them all in lexicographical order using sort(). Then we have to Concatenate the strings of string array in another empty string. Create an empty string result. To it add all the substrings of the array.

Finally return the string result. The time complexity of this solution is O(n2) as we use a double loop here.

std::string::substr. string substr (size_t pos = 0, size_t len = npos) const;.

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.

#include <string.h> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    // Take any string 
    string s1 = "Geeks"; 
  
    // 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: eek

Returns a substring [pos, pos+count) . If the requested substring extends past the end of the string, or if count == npos.

Use string.substr() function

  1. How to use string.substr() function?

    If I am correct, the second parameter of substr() should be the length of the substring. How about
    b = a.substr(i,2);

String to int c++

The substr() function returns a substring of the current string, starting at index, and length characters long. If length is omitted, it will default to string::npos.

Stoi c++

std::stoi · Parameters str String object with the representation of an integral number. idx Pointer to an object of type size_t

String::find c++

Use std::string::find as follows:
if (s1.find(s2) != std::string::npos) { std::cout << "found!" << '\n'; }
Note: “found!” will be printed if s2 is a substring of s1, both s1 and s2 are of type std::string.

You might see some C++ programs that use the size() function to get the length of a string. This is just an alias of length() .
To get the length of a string, use the length() function:
Example:

#include <iostream>
#include <string>
using namespace std;

int main() {
  string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  cout << "The length of the txt string is: " << txt.length();
  return 0;
}