substring function in C++ | Extract Substring Program C++

Written by

Juhi Kamdar

The small parts of a string, are known as substrings. These substrings can be of any length, but, they need to be in order. We cannot skip any letters in between while choosing the substrings.

For example:

Sun={s,u,n,su,un,sun} are substrings.
while, {sn,ns,nu,us,nus,uns} cannot be called as substring of it.

There are two ways to do this:

  1. When the starting index and length of substring is given.
  2. When the starting index and length of substring is not given.

In the first approach, We have two methods

  1. Using a user-defined function.
  2. Without using function.

In the second approach, we have one method

  1. Print all substrings.

Method 1: Using user-defined function, when starting index and length is given

Logic:

In this method, we take the starting index of substring and length from user. We pass these values in the function. In the function, using loop, we copy the characters from str to substr string, till the length inputted by user.

Algorithm:

  1. Take string input in str
  2. Store length of string in len
  3. Next, get the starting index from user as, start
  4. Get starting indes from user as endlen
  5. Call the functions after checking the necessary constraints
  6. In the function, take a for loop from start to endlen
  7. Initialize another string as substr, copy characters of str in substr.
  8. Print substr.

Code:

#include <iostream>
#include <string>
using namespace std;
void substring(string str, int start, int length)
{
	int i=start, j;
	string substr;
	for(j = 0; str[i] !='\0' && length > 0; i++, j ++)
		{
			substr[j] = str[i];
			length--;
		}
	substr[j] = '\0';        
cout<<"\n";
	for(int k=0;substr[k]!='\0';k++) 
	    cout<<substr[k];
}
int main()
{
	string str;
	int start,endlen,len;
	cout<<"Enter a string: ";
	getline(cin,str);
	len=str.length();
	cout<<"\n Enter starting position of substring : ";
	cin>>start ;
	cout<<"\n Enter length of substring: " ;
	cin>>endlen;
	if(start > 0 && start < 30 && endlen<len )
		substring(str,start,endlen);
	else
		cout<<"Values are invalid\n";
	return 0;
}

Output:

Enter a string: GoodMorning
Enter starting position of substring :4
Enter length of substring: 7

Morning

 

Method 2: Without Using user-defined function, when starting index and length is given

Logic:

this method, is similar to what previously we saw. The difference is that we do not use function here.

Algorithm:

  1. Take string input in str
  2. Store length of string in len
  3. Next, get the starting index from user as, start
  4. Get starting indes from user as endlen
  5. Check the necessary constraints
  6. If values are bounded by the constraints, take a for loop from start to endlen
  7. Initialize another string as substr, copy characters of str in substr.
  8. Print substr.

Code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str,substr;
	int start,endlen,len;
	cout<<"Enter a string: ";
	getline(cin,str);
	len=str.length();
	cout<<"\n Enter starting position of substring : ";
	cin>>start ;
	cout<<"\n Enter length of substring: " ;
	cin>>endlen;
	if(start > 0 && start < 30 && endlen<len )
	{
		int i=start,stopping_position;
		 cout<<"\n";
		for(int j = 0; i<len && endlen > 0; i++, j ++)
		{
			substr[j] = str[i];
			endlen--;
			stopping_position=j;
		}
		substr[stopping_position+1] = '\0';           
    for(int k=0;substr[k]!='\0';k++)
        cout&lt;&lt;substr[k];
}
else
	cout&lt;&lt;"Values are invalid\n";
return 0;

}

Output:

Enter a string: TomandJerry
Enter starting position of substring : 6
Enter length of substring: 5
Jerry

Method 3: Print all substrings

Logic:

In this method, we print all the possible substrings. We take length 1, and print all substrings with length 1, then we take 2 and print all the substrings with length 2. In this way, we progress, till we reach the length of the substrings.

Algorithm: 

  1. Take string input
  2. Store its length
  3. Run a for loop till the end of string, to get the starting point of a substring
  4. Nest another loop inside it, which keeps the ending point of a substring
  5. Now, nest third loop to it, such that, we print all the characters determined by the starting and ending point.

Code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str;
	int len;
	cout<<"Enter a string: ";
	getline(cin,str);
	len=str.length();
for(int i=1; i&lt;=len; i++)
{
	for (int j = 0; j &lt;= (len-i); j++)
	{
    	cout&lt;&lt;"\n";	
    	for (int k = j; k &lt;= i+j-1; k++)
    		cout&lt;&lt;str[k];
	}
}

return 0; }

Output:

Enter a string: Help
H
e
l
p
He
el
lp
Hel
elp
Help
substring function in C++ | Extract Substring Program C++