In: Computer Science
Using the following text string: axcbramneltodaydy
Search for the following pattern: 'today' Specify whether or not you found the pattern. The minimum requirement is to use the string STL, the brute force method and hard code both the text and the pattern.
For extra credit, you can add any of the following:
1. Do not use the string STL.
2. Allow me to enter both the text string and the pattern, including spaces. You can designate a special end-of-text character for me to include.
3. Use the Boyer-Moore algorithm instead of brute force.
4. Identify the location where the pattern was found within the text.
5. Allow for the possibility that the pattern can appear more than once in the text and identify all of the locations where the pattern was found.
Amongst al the requirements mentioned for extra credits, i am allowing user to enter thetext and pattern manually
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
//declare 2 strings to take input
string text, pattern;
//Take user input
cout<<"Enter text: ";
getline(cin, text);
cout<<"Enter pattern: ";
getline(cin, pattern);
if(text.find(pattern)!=-1) //pattern is found
cout<<"pattern is found";
else
cout<<"pattern is not found";
return 0;
}
Below is the screenshot of output -

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any