- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find strings that end with a given suffix
C++ has a predefined function substr to return the portion of string and compare function to check the sequence of characters. The suffix means the group of characters added to the end of the word.
In this article, we are going to find the strings that end with a given suffix.
Let us understand the example of suffix by taking some of the strings −
Tutorialspoint − The characters n and t represent the suffix.
Tutorix − The characters r, i, and x represent the suffix.
Note that, the length of the reverse of some characters in the word is termed a suffix.
Syntax
Substr()
This function is used to calculate the length of a character in a string by defining input to the string.
compare()
This function is used to compare the matches of characters in the given string or substring. If the match's character is satisfied, then it will return 0.
Algorithm
We will start the program with header files namely ‘iostream’ and ‘string’.
After that, we will start the main function and declare the string value to the variable ‘Ecom’.
Later on we initialize the size of the ‘Ecom’ array to variable ‘n.
Now we are using the same logic by giving different loops in the examples and do the following −
Ecom[i].substr(Ecom[i].length()-total_character_in_number).compare("suffix_name")==0
In example 1, we are using the for loop to iterate each index of string ‘Ecom’.
In example 2, we are using the while loop to iterate each index of string ‘Ecom’.
In example 1 and example 2, we are using the if-statement to denote two methods- substr() and compare() to Ecom[i] which verifies the length of the suffix up to some characters, and by taking that character to compare method it will set the character equivalent to 0 which will found to return the given suffix.
Finally, we are printing the output statement with the help of the string ‘Ecom[i]’.
Example 1
ln this program, we are going to execute the string that ends with a given suffix by using for loop.
#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; }
Output
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
Example 2
ln this program, we are going to execute the string that ends with a given suffix by using a while loop.
#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; }
Output
The suffix ful used in the string: Colorful The suffix ful used in the string: Beautiful
Conclusion
We explored the concept of a string that ends with a given suffix. We have seen how the 'substr()' and 'compare()' methods find similar suffix characters in multiple strings. On the other side, we will also apply the same concept to the prefix program. This program helps to build applications such as search boxes on the web, spreadsheet searches, metadata used in SEO, etc.