C has a predefined function substr to return a part of a string and a comparison function to check character sequences. A suffix represents a group of characters added to the end of a word.
In this article, we will find strings ending with a given suffix.
Let us understand the example of suffix through some strings -
Tutorialspoint - The characters n and t represent suffixes.
Tutorix - The characters r, i and x represent suffixes.
Please note that the reverse length of certain characters in a word is called a suffix.
Substr()
This function is used to calculate the length of characters in a string by defining the input of the string.
compare()
This function is used to compare the matching of characters in a given string or substring. If the matching characters are satisfied, 0 is returned.
We will use the header files 'iostream' and 'string' to start the program.
After that, we will start the main function and declare the string value to the variable 'Ecom'.
Later we initialize the size of the 'Ecom' array to the variable 'n.
Now we use the same logic by giving different loops in the example and do the following -
Ecom[i].substr(Ecom[i].length()-total_character_in_number).compare("suffix_name")==0
In Example 1, we use a for loop to iterate through each index of the string 'Ecom'.
In Example 2, we use a while loop to iterate through each index of the string 'Ecom'.
In Example 1 and Example 2, we use if statements to represent two methods - substr() and compare() to Ecom[i ] It verifies that the suffix is of length at most certain characters and by comparing that character to the comparison method it will set that character equal to 0 which will return the given suffix.
Finally, we use the string ‘Ecom[i]’ to print the output statement.
In this program, we will use for loop to execute the string ending with the given suffix.
#include <iostream> #include <string> using namespace std; int main(){ string Ecom[6] = {"Myntra","Synasera","Myra","Condera","Reseme","Beautiful"}; int n = sizeof(Ecom)/sizeof(Ecom[0]); for(int i = 0; i < n; i++) { if(Ecom[i].substr(Ecom[i].length() - 2).compare("ra") == 0) { cout<<"The suffix ra used in the string: "<<Ecom[i]<<endl; } } return 0; }
The suffix ra used in the string: Myntra The suffix ra used in the string: Synasera The suffix ra used in the string: Myra The suffix ra used in the string: Condera
In this program, we will use while loop to execute the string ending with the given suffix.
#include<iostream> #include<string> using namespace std; int main() { string Ecom[6] = {"Myntra","Synasera","Myra","Colorful","Reseme","Beautiful"}; int n = sizeof(Ecom)/sizeof(Ecom[0]); int i; while(i < n) { if(Ecom[i].substr(Ecom[i].length() - 3).compare("ful") == 0) { cout<<"The suffix ful used in the string: "<<Ecom[i]<<endl; } i++; } return 0; }
The suffix ful used in the string: Colorful The suffix ful used in the string: Beautiful
We explored the concept of a string ending with a given suffix. We have seen how the "substr()" and "compare()" methods find similar suffix characters in multiple strings. On the other hand, we also apply the same concept to prefix programs. The program helps in building applications such as web search boxes, spreadsheet searches, metadata used in SEO, and more.
The above is the detailed content of Find strings ending with a given suffix. For more information, please follow other related articles on the PHP Chinese website!