$ g++ RepeatedSearch.cpp $ a.out NOTE:Strings are accepted only till blank space. Enter Original String:ThisC++programperformsnaivestringmatchingwithoutusinganyspecificlibraryfunctions.Atextandapatternisgivenasinput.Thepatternissearchedforinthetextandallinstancesofthepatternaregivenasoutput.h Enter Pattern to Search:in Pattern Found at Position: 30 Pattern Found at Position: 38 Pattern Found at Position: 50 Pattern Found at Position: 100 Total Instances Found = 4 ------------------ (program exited with code: 0) Press return to continue
Users Online
· Guests Online: 93
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C++ Program to Repeatedly Search the Same Text
C++ Program to Repeatedly Search the Same Text
This C++ program performs naive string matching without using any specific library functions. A text and a pattern is given as input. The pattern is searched for in the text and all instances of the pattern are given as output.
Here is source code of the C++ Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure). The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
-
//enter string without spaces
-
#include<iostream>
-
#include<string.h>
-
using namespace std;
-
-
int main()
-
{
-
char org[100], dup[100];
-
int i, j, k = 0, len_org, len_dup;
-
cout << "NOTE:Strings are accepted only till blank space.";
-
cout << "\nEnter Original String:";
-
fflush(stdin);
-
cin >> org;
-
fflush(stdin);
-
cout << "Enter Pattern to Search:";
-
cin >> dup;
-
-
len_org = strlen(org);
-
len_dup = strlen(dup);
-
for (i = 0; i <= (len_org - len_dup); i++)
-
{
-
for (j = 0; j < len_dup; j++)
-
{
-
//cout<<"comparing '"<<org[i + j]<<"' and '"<<dup[j]<<"'.";
-
if (org[i + j] != dup[j])
-
break;
-
}
-
if (j == len_dup)
-
{
-
k++;
-
cout << "\nPattern Found at Position: " << i;
-
}
-
}
-
if (k == 0)
-
cout << "\nError:No Match Found!";
-
else
-
cout << "\nTotal Instances Found = " << k;
-
return 0;
-
}
Output:
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.