substring function in C++ | Extract Substring Program C++
Written by
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:
- When the starting index and length of substring is given.
- When the starting index and length of substring is not given.
In the first approach, We have two methods
- Using a user-defined function.
- Without using function.
In the second approach, we have one method
- 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:
- Take string input in str
- Store length of string in len
- Next, get the starting index from user as, start
- Get starting indes from user as endlen
- Call the functions after checking the necessary constraints
- In the function, take a for loop from start to endlen
- Initialize another string as substr, copy characters of str in substr.
- 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:
- Take string input in str
- Store length of string in len
- Next, get the starting index from user as, start
- Get starting indes from user as endlen
- Check the necessary constraints
- If values are bounded by the constraints, take a for loop from start to endlen
- Initialize another string as substr, copy characters of str in substr.
- 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<<substr[k];
}
else
cout<<"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:
- Take string input
- Store its length
- Run a for loop till the end of string, to get the starting point of a substring
- Nest another loop inside it, which keeps the ending point of a substring
- 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<=len; i++)
{
for (int j = 0; j <= (len-i); j++)
{
cout<<"\n";
for (int k = j; k <= i+j-1; k++)
cout<<str[k];
}
}
return 0;
}
Output:
Enter a string: Help
H
e
l
p
He
el
lp
Hel
elp
Help