I have a homework assignment on a compiler called zybooks. It has an automatic grader. I get the right output however for two of my functions is outputs the error.
0 / 2
Test that ReplaceExclamation() works correctly with "We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!"
Your output
Edited text: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue.
Test feedback
ReplaceExclamation() incorrectly edits the string.
Result: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
What am I doing wrong?
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
|
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <math.h>
#include <stdio.h>
using namespace std;
void ReplaceExclamation(string userInput) {
string newText = userInput;
int i = 0;
int len = userInput.size();
for (i = 0; i < len; i++) {
if (userInput[i] == '!') {
newText[i] = '.';
}
userInput = newText;
}
cout << "Edited text: " << userInput << endl;
}
int FindText(string userInput, string phrase) {
int count = 0;
size_t offset = userInput.find(phrase);
if (phrase.size() == 0) {
return 0;
}
while (userInput.find(phrase, offset) != string::npos)
{
offset = userInput.find(phrase, offset + phrase.size());
count++;
}
return count;
}
void ShortenSpace(string userInput) {
int i, j,c = 0, n;
n = userInput.size();
while (i < n)
{
if (i == ' ' && (i + 1) == ' ' || (i - 1) == ' ')
{
for (j = i; j < n; j++) {
j = j + 1;
n--;
}
}
else {
i++;
}
}
cout << "Edited text: " << userInput << endl;
}
int GetNumOfWords(const string userInput)
{
int words = 0;
int len = userInput.size();
int i = 0;
for (i = 0; i<len;)
{
if (isspace(userInput[i]))
{
while (isspace(userInput[i]))
i++;
words++;
}
else
{
i++;
}
}
words = words + 1;
cout << "Number of words: " << words << endl;
return words;
}
int GetNumOfNonWSCharacters(const string userInput) {
int i = 0;
int numOfCharacters = 0;
int length = userInput.size();
for (i = 0; i < length; ++i) {
if (!isspace(userInput[i])) {
numOfCharacters++;
}
}
cout << "Number of non-whitespace characters: " << numOfCharacters << endl << endl;
return numOfCharacters;
}
void printMenu(string userInput) {
cout << "MENU" << endl;
cout << "c - Number of non-whitespace characters" << endl;
cout << "w - Number of words" << endl;
cout << "f - Find text" << endl;
cout << "r - Replace all !'s" << endl;
cout << "s - Shorten spaces" << endl;
cout << "q - Quit" << endl << endl;
cout << "Choose an option: " << endl;
}
int main() {
string phrase;
string userInput;
string numOfCharacters;
string userLetter;
cout << "Enter a sample text: " << endl;
getline(cin, userInput);
cout << "You entered: " << endl;
cout << userInput << endl;
do {
printMenu(userLetter);
cin >> userLetter;
if (userLetter == "w") {
GetNumOfWords(userInput);
}
else if (userLetter == "f") {
cout << "Enter a word or phrase to be found: " << endl;
cin.ignore();
getline(cin, phrase);
cout << "\" " << phrase << "\" " << "instances: " << FindText(userInput, phrase) << endl;
}
else if (userLetter == "r") {
ReplaceExclamation(userInput);
}
else if (userLetter == "s") {
ShortenSpace(userInput);
}
else if (userLetter == "c") {
GetNumOfNonWSCharacters(userInput);
}
else if (userLetter == "q") {
}
else {
cout << "Please select a valid character." << endl;
}
} while (userLetter != "q");
return 0;
}
|