(C++) String manipulation - need to get number of words. Hello, I\'m writing a s
ID: 3887164 • Letter: #
Question
(C++) String manipulation - need to get number of words.
Hello, I'm writing a string manipulation code and need to have an option to read the number of words typed by the user. I thought I wrote it correctly but each time I keep getting a very large number even though I'm only entering maybe two words. Any help would be appreciated, thank you.
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
int GetNumOfNonWSCharacters(string);
int GetNumOfWords(string);
int FindText(string, string);
string ReplaceExclamation(string);
string ShortenSpace(string);
char printMenu();
char option;
string text, phraseToFind;
cout << "Enter a sample text:";
getline (cin, text);
cout << "You entered:" << text;
while(1) {
option = printMenu();
switch(option) {
case 'q':
case 'Q':
exit(0);
break;
case 'c':
case 'C':
cout << "Number of non-whitespace characters:" << GetNumOfNonWSCharacters(text);
break;
case 'w':
case 'W':
cout << "Number of words:" << GetNumOfWords(text);
break;
case 'f':
case 'F':
cout << "Enter a word or phrase to find: ";
getline(cin, phraseToFind);
cout << phraseToFind << " instances: " << FindText(text, phraseToFind);
break;
case 'r':
case 'R':
text = ReplaceExclamation(text);
cout << "Edited text: " << text;
case 's':
case 'S':
text = ShortenSpace(text);
cout << "Edited text: " << text;
break;
default:
cout << "Invalid choice... Try again.";
break;
}
}
return 0;
}
char printMenu() {
char ch;
cout << " Menu Options: ";
cout << " c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit";
cout <<" Choose an option: ";
cin >> ch;
return ch;
}
int GetNumOfNonWSCharacters(const string text) {
int cnt = 0, i;
int len = text.size();
for (i = 0; i < len; i++) {
if(!isspace(text[i])) {
cnt++;
}
}
return cnt;
}
int GetNumOfWords(const string text) {
int i;
int words;
int len = text.size();
for (i = 0; i < len;) {
if (isspace(text[i])) {
while (isspace(text[i]))
i++;
words++;
}
else {
i++;
}
}
return words;
}
string ReplaceExclamation(string text) {
string newText = text;
int i, len = text.size();
for (i = 0; i < len; i++) {
if (text[i] == '!')
newText[i] = '.';
}
return newText;
}
string ShortenSpace(string text) {
char *newText;
int i, len = text.size(), k = 0;
newText = new char [len + 1];
for (i = 0; i < len; k++) {
newText[k] = text[i];
if (isspace(text[i])) {
while (isspace(text[i]))
i++;
} else {
i++;
}
}
newText[k] = '';
return newText;
}
int FindText (string text, string phrase) {
int count = 0;
if (phrase.size() == 0)
return 0;
for(size_t offset = text.find(phrase); offset != std::string::npos; offset = text.find(phrase, offset + phrase.size())) {
++count;
}
return count;
}
Explanation / Answer
//Please find the updated code below I have corrected the logic for get number of words , Please do thumbs up if
// you like the solution
#include <iostream>
#include <string>
using namespace std;
int main() {
int GetNumOfNonWSCharacters(string);
int GetNumOfWords(string);
int FindText(string, string);
string ReplaceExclamation(string);
string ShortenSpace(string);
char printMenu();
char option;
string text, phraseToFind;
cout << "Enter a sample text:";
getline (cin, text);
cout << "You entered:" << text;
while(1) {
option = printMenu();
switch(option) {
case 'q':
case 'Q':
exit(0);
break;
case 'c':
case 'C':
cout << "Number of non-whitespace characters:" << GetNumOfNonWSCharacters(text);
break;
case 'w':
case 'W':
cout << "Number of words:" << GetNumOfWords(text);
break;
case 'f':
case 'F':
cout << "Enter a word or phrase to find: ";
getline(cin, phraseToFind);
cout << phraseToFind << " instances: " << FindText(text, phraseToFind);
break;
case 'r':
case 'R':
text = ReplaceExclamation(text);
cout << "Edited text: " << text;
case 's':
case 'S':
text = ShortenSpace(text);
cout << "Edited text: " << text;
break;
default:
cout << "Invalid choice... Try again.";
break;
}
}
return 0;
}
char printMenu() {
char ch;
cout << " Menu Options: ";
cout << " c - Number of non-whitespace characters w - Number of words f - Find text r - Replace all !'s s - Shorten spaces q - Quit";
cout <<" Choose an option: ";
cin >> ch;
return ch;
}
int GetNumOfNonWSCharacters(const string text) {
int cnt = 0, i;
int len = text.size();
for (i = 0; i < len; i++) {
if(!isspace(text[i])) {
cnt++;
}
}
return cnt;
}
int GetNumOfWords(const string text) {
int i;
int words=0;
int len = text.size();
for (i = 0; i < len; i++)
{
if (isspace(text[i]))
{
words++;
}
}
return words+1;
}
string ReplaceExclamation(string text) {
string newText = text;
int i, len = text.size();
for (i = 0; i < len; i++) {
if (text[i] == '!')
newText[i] = '.';
}
return newText;
}
string ShortenSpace(string text) {
char *newText;
int i, len = text.size(), k = 0;
newText = new char [len + 1];
for (i = 0; i < len; k++) {
newText[k] = text[i];
if (isspace(text[i])) {
while (isspace(text[i]))
i++;
} else {
i++;
}
}
newText[k] = '';
return newText;
}
int FindText (string text, string phrase) {
int count = 0;
if (phrase.size() == 0)
return 0;
for(size_t offset = text.find(phrase); offset != std::string::npos; offset = text.find(phrase, offset + phrase.size())) {
++count;
}
return count;
}
OUTPUT:
Enter a sample text:My Name is ABC
You entered:My Name is ABC
Menu Options:
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option: w
Number of words:4
Menu Options:
c - Number of non-whitespace characters
w - Number of words
f - Find text
r - Replace all !'s
s - Shorten spaces
q - Quit
Choose an option:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.