This repository was archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestRecursion.cpp
More file actions
171 lines (142 loc) · 7.61 KB
/
Copy pathtestRecursion.cpp
File metadata and controls
171 lines (142 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*----------------------------------------------------------------------------+
| Author: Jerome Richards |
| |
| Assignment: Project 8 |
| File: testRecusion.cpp |
| |
| Instructor: Dr. Roger deBry |
| Class: CNS-1350 Section X01 |
| |
| Date Written: February 28, 2005 |
| |
| Description: |
| |
| Test the use of recursion to locate a substring within another string |
+----------------------------------------------------------------------------*/
#include <iostream>
#include <cassert>
#include <string>
using namespace std;
const int FALSE = 0;
const int TRUE = 1;
const char NULL_CHAR = 0x00;
void dispPrologue();
int index_of( string theSource, string theSearch );
bool compareStr( const char* source, const char* search );
/*----------------------------------------------------------------------------+
| Purpose: Test the substring search |
| |
| Parameters: argc an int representing the number of parameters |
| |
| argv an array of character parameters |
| |
| Returns: 0 representing successful execution |
| |
| Pre-conditions: none |
| Post-conditions: none |
+----------------------------------------------------------------------------*/
int main( int argc, char* argv[] )
{
string tmpString;
string tmpSearch;
dispPrologue();
cout << "Enter in a string - no spaces please.\n";
cin >> tmpString;
cout << "\n\nEnter in a second string to search for in " << tmpString << ".\n";
cin >> tmpSearch;
int startingPos = index_of( tmpString, tmpSearch );
cout << "\n\nThe starting point of " << tmpSearch
<< " in " << tmpString << " is ";
if( startingPos < 0 )
cout << "Not found" << "\n\n";
else
cout << " = " << startingPos << "\n\n";
system( "pause" );
return 0;
}
/*----------------------------------------------------------------------------+
| Purpose: Locate a substring within another string |
| |
| Parameters: theSource, a string, containing the string to be searched |
| theSearch, a string, the substring to search for |
| |
| Returns: -1 if the substring is not found |
| |
| int representing the starting position in the search |
| string |
| |
| Pre-conditions: theSource and theSearch parameters are valid string |
| objects |
| |
| Post-conditions: none |
+----------------------------------------------------------------------------*/
int index_of( string theSource, string theSearch )
{
int startingPos = -1;
int nLen = (int) theSource.length();
const char* source = theSource.c_str();
const char* search = theSearch.c_str();
for( startingPos = 0; startingPos < nLen; startingPos++ )
{
// find where the first character of the Search string is in the target string
if( *search == *source )
{
if( compareStr( source, search ) )
return startingPos + 1;
}
source++;
}
return -1;
}
/*----------------------------------------------------------------------------+
| Purpose: Called recursively to check character for character match |
| between the SOURCE string and the SEARCH string |
| |
| Parameters: source, a const char*, pointing to the next character to |
| compare against the search string |
| |
| search, a const char*, pointing to the next character to |
| compare against the source string |
| |
| Returns: TRUE only if the SOURCE contains the SEARCH string |
| |
| FALSE if not found |
| |
| Pre-conditions: source and search parameters are valid const char * |
| |
| Post-conditions: none |
+----------------------------------------------------------------------------*/
bool compareStr( const char* source, const char* search )
{
bool lFound = FALSE;
// if we are at the end of Search string we are done
// - and the substring is found
if( *search == NULL_CHAR )
return TRUE;
// if we are at the end of the Source String we are done
// - and the substring has not been found
if( *source == NULL_CHAR )
return FALSE;
// if both first characters are equal then we can continue comparing
if( *source == *search )
lFound = compareStr( ++source, ++search );
return lFound;
}
/*----------------------------------------------------------------------------+
| Purpose: Display file prologue |
| Parameters: none |
| Returns: nothing |
| |
| Pre-conditions: none |
| Post-conditions: none |
+----------------------------------------------------------------------------*/
void dispPrologue()
{
system( "cls" );
cout << " Name: Jerome Richards\n";
cout << " Course: CNS-1350 Section X01\n";
cout << "Instructor: Dr. Roger deBry\n";
cout << " Program: Project Eight\n";
cout << " File: Substring Search (testRecusion.exe)\n";
cout << "\n\n";
}