I need the following functions in C++ I need a C++ Program that has the followin
ID: 3863417 • Letter: I
Question
I need the following functions in C++
I need a C++ Program that has the following functions:
1. wordReport counts the words found in the first argument. The return value of the function is the number of words found. For this function, words are considered a sequence of one or more alphanumerics, separated by one or more non-alphanumeric characters.
2. countOccurrences searches for the text provided as the second c-string argument within the first function argument. The number of matches found is returned. e.g. countOccurrences("abcd", "bc") returns 1, and countOccurrences("aaaaaaa", "aaa") would return 2 (not 5),
3. countOccurrencesList searches the first c-string as for countOccurrences, except the second argument is an array of c-string pointers, and a third integer argument giving a count of the number of pointers in the array. A single value is returned which is the accumulated count of all occurrences of the words found in the first c-string.
4. replaceText replaces all occurrences of text found in the second argument, with the third argument (must be of equal or lesser size). Returns the number of replacements made. Returns 0 if the replacement c-string is longer than the second argument. Watch out: you could get an infinite loop if the search string is found within the replacement string, and you attempt to re-process from the start of the string.
Write the functions as described above. Make sure all required header files are included at the top of your program file.You may wish to write a main() program to test your function initially before proceeding with the following steps. If you do so, you must remove it or comment it out before continuing.Since you will not be submitting the main() function, You will be compiling your program by doing the following:Type your program code into Code::BlocksSave the linkable testdriver strMain.a to the same folder where your C++ program file is. (Right-click, then Save Link As ... or similar.)In Code::Blocks, go to the Settings menu, and select Compiler to bring up a dialog boxMake sure the Global Compiler Settings is selected on the left, then click on the Linker Settings tabJust below the Link libraries box, click the Add button, then navigate to where you saved strMain.a. Click Ok to close the library selection dialog box, and then Ok to close the Global Compiler Settings.You can now Compile and Run your program using the test driver. If your program does not compile, then you probably have not defined your function correctly, or forgot to include all appropriate header files.When running with the test driver, the program output will tell you if it is working correctly. Your program will be evaluated using the exact same test driver.Submit only the file containing your functions. You should not provide any test main() that you may have written for your own test purposes. When you submit your program on the class website, you should receive no compiler or linker errors.
You may use any function from the cctype library. From the cstring library you may use only strlen(), strstr(), and strncmp(). And, as for all assignments in this class, the C++ string class may not be used. The first argument to all functions that you will implement below is a c-string, and in most cases you will modify it, i.e. it is both a function input and output. All input-only parameters should be declared as constto indicate that the character array will not be modified. Note that word may be defined differently for various functions. For the purposes of matching, assume upper/lower case is significant, so Apple is not the same as apple.
Here is the test driver: https://drive.google.com/file/d/0B0Jl2I-W5UEPSDdyTFNtdDI2MWc/view?usp=sharing
Thank you!!!
Explanation / Answer
1)
void replace_multi_space_with_single_space(char *str)
{
char *dest = str; /* Destination to copy to */
while (*str != '')
{
while (*str == ' ' && *(str + 1) == ' ')
str++;
*dest++ = *str++;
}
*dest = '';
}
2)
#include <iostream>
#include <string>
nt countSubstring(const std::string& str, const std::string& sub)
{
if (sub.length() == 0) return 0;
int count = 0;
for (size_t offset = str.find(sub); offset != std::string::npos;
offset = str.find(sub, offset + sub.length()))
{
++count;
}
return count;
}
4)
#include <iostream>
#include <string>
using namespace std;
int main() // Replacing comma with "|" in the string holdLetter and counting how many times the replacement is made.
{
int count = 0; // for the number of occurences.
// final hold variable of corrected word up to the npos=j
string holdWord = "";
// a temp var in order to replace 0 to new npos
string holdTemp = "";
// a csv for a an entry in a book store
string holdLetter = "This is a, horrible story ,about beauty and beast ,99.85";
// j = npos
for (int j = 0; j < holdLetter.length(); j++) {
if (holdLetter[j] == ',') {
if ( count == 0 )
{
holdWord = holdLetter.replace(j, 1, " | ");
}
else {
string holdTemp1 = holdLetter.replace(j, 1, " | ");
// since replacement is three positions in length,
// must replace new replacement's 0 to npos-3, with
// the 0 to npos - 3 of the old replacement
holdTemp = holdTemp1.replace(0, j-3, holdWord, 0, j-3);
holdWord = "";
holdWord = holdTemp;
}
holdTemp = "";
count++;
}
}
cout << holdWord << count << endl;
return 0;
}
5)
void reverseWords(char *s) // char s[] = "Do i say";
replace_multi_space_with_single_space(s);
char *temp = s;
reverseWords(s);
{
char *word_begin = s;
char *temp = s;
while( *temp )
{
temp++;
if (*temp == '')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
}
reverse(s, temp-1);
}
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.