In: Computer Science
C++ I took 7/20 =(
code:
#include <iostream>
#include<string.h>
using namespace std;
// function to calculate number non white space characters
int GetNumOfNonWSCharacters(string str) {
int i = 0;
int count = 0;
while(str[i] != '\0') {
if(str[i] != ' ') {
count += 1;
}
i++;
}
return count;
}
// function to calculate numbers of words
int GetNumOfWords(string str) {
int i = 0;
int count = 1;
while(str[i] != '\0') {
if(str[i] == ' ' && str[i-1] != ' ') {
count += 1;
}
i++;
}
return count;
}
// function to find the number of occurence of the word
int FindText(string word, string str) {
int i = 0;
int count = 0;
while(str[i] != '\0') {
if(str[i] == word[0]) {
int k = i;
int j = 0;
while(word[j] != '\0') {
if(word[j] != str[k]) {
break;
}
if(word[j+1] == '\0') {
count += 1;
}
k++;
j++;
}
}
i++;
}
return count;
}
// function to replace the '!' in string
void ReplaceExclamation(string str) {
int i = 0;
string edited;
while(str[i] != '\0') {
if(str[i] == '!') {
edited[i] = '.';
}
else {
edited[i] = str[i];
}
i++;
}
edited[i] = '\0';
// display resulting string
int j = 0;
cout << "Edited Text:\n" << endl;
while(edited[j] != '\0') {
cout << edited[j];
j++;
}
cout << endl;
}
// function to remove extra spaces
void ShortenSpace(string str) {
int i = 0;
int j = 0;
int len = str.length();
string edited;
while(str[i] != '\0') {
if(str[i] != ' ') {
edited[j] = str[i];
j++;
}
else {
if(edited[j-1] != ' ') {
edited[j] = ' ';
j++;
}
}
i++;
}
edited[j] = '\0';
// display resulting string
int k = 0;
cout << "Edited Text:\n" << endl;
while(edited[k] != '\0') {
cout << edited[k];
k++;
}
cout << endl;
}
// function to display menu options
void PrintMenu(string str) {
while(1) {
// display menu
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;
// read user option
char option;
cout << "Choose an option: ";
cin >> option;
// validate input
while(1) {
if(option != 'c' && option != 'w' && option != 'r'
&& option != 's' && option != 'q' && option
!= 'f') {
cout << "Choose an option: ";
cin >> option;
}
else{
break;
}
}
// perform the required operation
if(option == 'q') {
break;
}
else if(option == 'c') {
int non_whitespace_chars = GetNumOfNonWSCharacters(str);
cout << "Number of non-whitespace characters: " <<
non_whitespace_chars << endl;
}
else if(option == 'w') {
int num_words = GetNumOfWords(str);
cout << "Number of words: " << num_words <<
endl;
}
else if(option == 'f') {
string word;
cin.ignore();
getline(cin, word);
int finds = FindText(word, str);
cout << "Number of words or phrases found: " << finds
<< endl;
}
else if(option == 'r') {
ReplaceExclamation(str);
}
else {
ShortenSpace(str);
}
cout << endl;
}
}
// main function
int main() {
// read input text and output it
string text;
cout << "Enter sample text:" << endl;
getline(cin, text);
cout << "You entered:\n" << text << endl;
// call print menu function
PrintMenu(text);
return 0;
}
(1) Prompt the user to enter a string of their choosing. Store
the text in a string. Output the string. (1 pt)
Ex:
Enter a sample 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! You entered: 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!
(2) Implement a PrintMenu() function, which has a string as a
parameter, outputs a menu of user options for analyzing/editing the
string, and returns the user's entered menu option. Each option is
represented by a single character.
If an invalid character is entered, continue to prompt for a
valid choice. Hint: Implement Quit before implementing other
options. Call PrintMenu() in the main() function. Continue to
call PrintMenu() until the user enters q to Quit. (3 pts)
Ex:
MENU 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:
(3) Implement the GetNumOfNonWSCharacters() function.
GetNumOfNonWSCharacters() has a constant string as a parameter and
returns the number of characters in the string, excluding all
whitespace. Call GetNumOfNonWSCharacters() in the PrintMenu()
function. (4 pts)
Ex:
Number of non-whitespace characters: 181
(4) Implement the GetNumOfWords() function. GetNumOfWords() has a
constant string as a parameter and returns the number of words in
the string. Hint: Words end when a space is reached except for
the last word in a sentence. Call GetNumOfWords() in the
PrintMenu() function. (3 pts)
Ex:
Number of words: 35
(5) Implement the FindText() function, which has two strings as
parameters. The first parameter is the text to be found in the user
provided sample text, and the second parameter is the user provided
sample text. The function returns the number of instances a word or
phrase is found in the string. In the PrintMenu() function, prompt
the user for a word or phrase to be found and then call FindText()
in the PrintMenu() function. Before the prompt, call
cin.ignore() to allow the user to input a new
string. (3 pts)
Ex:
Enter a word or phrase to be found: more "more" instances: 5
(6) Implement the ReplaceExclamation() function.
ReplaceExclamation() has a string parameter and updates the string
by replacing each '!' character in the string with a '.' character.
ReplaceExclamation() DOES NOT output the string. Call
ReplaceExclamation() in the PrintMenu() function, and then output
the edited string. (3 pts)
Ex.
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.
(7) Implement the ShortenSpace() function. ShortenSpace() has a
string parameter and updates the string by replacing all sequences
of 2 or more spaces with a single space. ShortenSpace() DOES NOT
output the string. Call ShortenSpace() in the PrintMenu() function,
and then output the edited string. (3 pt)
Ex:
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!
Note:
While executing function for replacing '!' with '.' and shortening the string I have not modified the original string but performed manipulations on a copy of the string.
#include <iostream>
#include <string>
using namespace std;
// function to calculate number non white space characters
int GetNumOfNonWSCharacters(string str)
{
int i = 0;
int count = 0;
while (str[i] != '\0') {
if (!isspace(str[i])) { //Considers all the non-whitespace charatcers
count += 1;
}
i++;
}
return count;
}
// function to calculate numbers of words
int GetNumOfWords(string str)
{
int i = 0;
int count = 1;
while (str[i] != '\0') {
if (str[i] == ' ' && str[i - 1] != ' ') {
count += 1;
}
i++;
}
return count;
}
// function to find the number of occurence of the word
int FindText(string word, string str)
{
int i = 0;
int count = 0;
while (str[i] != '\0') {
if (str[i] == word[0]) {
int k = i;
int j = 0;
while (word[j] != '\0') {
if (word[j] != str[k]) {
break;
}
if (word[j + 1] == '\0') {
count += 1;
}
k++;
j++;
}
}
i++;
}
return count;
}
// function to replace the '!' in string
void ReplaceExclamation(string str)
{
int i = 0;
string edited=str; //First copy the entire contents of the string to new one and then update the new string
while (str[i] != '\0') {
if (str[i] == '!') {
edited[i] = '.';
}
else {
edited[i] = str[i];
}
i++;
}
// display resulting string
int j = 0;
cout << "Edited Text:\n" << endl;
while (edited[j] != '\0') {
cout << edited[j];
j++;
}
cout << endl;
}
// function to remove extra spaces
void ShortenSpace(string str)
{
int i = 0;
int j = 0;
int len = str.length();
bool space=false;
string edited=str; //Copy entire contents of original string to new one
while(j < len){
if(edited[j] != ' '){
edited[i++]=edited[j++];
space=false; //no space found thus, setting variable to false
}
else if(edited[j++] == ' '){
if(!space){
edited[i++]=' '; //Only one space is appended in the string
space=true;
}
}
}
edited[i]='\0';
// display resulting string
int k = 0;
cout << "Edited Text:\n" << endl;
while (edited[k] != '\0') { //iterate in edited string only till coiunter i as i marks the end of edited string
cout << edited[k];
k++;
}
cout << endl;
}
// function to display menu options
void PrintMenu(string str)
{
while (1) {
// display menu
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;
// read user option
char option;
cout << "Choose an option: ";
cin >> option;
// validate input
while (1) {
if (option != 'c' && option != 'w' && option != 'r' && option != 's' && option != 'q' && option != 'f') {
cout << "Choose an option: ";
cin >> option;
}
else {
break;
}
}
// perform the required operation
if (option == 'q') {
break;
}
else if (option == 'c') {
int non_whitespace_chars = GetNumOfNonWSCharacters(str);
cout << "Number of non-whitespace characters: " << non_whitespace_chars << endl;
}
else if (option == 'w') {
int num_words = GetNumOfWords(str);
cout << "Number of words: " << num_words << endl;
}
else if (option == 'f') {
string word;
cin.ignore();
getline(cin, word);
int finds = FindText(word, str);
cout << "Number of words or phrases found: " << finds << endl;
}
else if (option == 'r') {
ReplaceExclamation(str);
}
else {
ShortenSpace(str);
}
cout << endl;
}
}
// main function
int main()
{
// read input text and output it
string text;
cout << "Enter sample text:\n" << endl;
getline(file, text);
cout << "\nYou entered:\n" << text << endl;
// call print menu function
PrintMenu(text);
return 0;
}
Attaching screenshots for the output of the program