Answer to Question #259464 in C++ for thugger

Question #259464

Write a C++ code to read a line of string as an input and do the following operations. Do proper input

validation to your input.

i) Capitalize first and last character of each word in the given string. [3 Marks]

ii) Delete a word from the given string. Popup a message if the string is empty. [5 Marks]

iii) Add a word in the middle of the given string. Capitalize the new word if it already exists. [5 Marks]


For proper input validation


1
Expert's answer
2021-10-31T18:41:09-0400
#include <iostream>
#include <string>
#include <cctype>
using namespace std;


const string delimiters = " \t\n.,;:\"\'()!?";


void capitalize_first_last(string& str) {
    
    if (str.size() == 0) {
        return;
    }


    size_t pos = 0, end;
    while (pos != string::npos) {
        pos = str.find_first_not_of(delimiters, pos);
        if (pos == string::npos) {
            break;
        }
        str[pos] = toupper(str[pos]);
        pos = str.find_first_of(delimiters, pos);
        end = (pos != string::npos ? pos : str.size()) - 1;
        str[end] = toupper(str[end]);
    }
}


bool delete_word(string& str, int n) {
    if (str.size() == 0) {
        return false;
    }


    size_t pos = 0, start = 0, end;
    int i = 0;
    while (pos != string::npos) {
        i++;
        pos = str.find_first_not_of(delimiters, pos);
        if (pos == string::npos) {
            break;
        }
        pos = str.find_first_of(delimiters, pos);
        end = pos;
        if ( i == n) {
            str.replace(start, end-start, "");
            return true;
        }
        start = pos;
    }
    return false;
}


void insert_word(string& str, int n, const string& word) {
    if (str.size() == 0) {
        return;
    }


    size_t pos = 0, start = 0, end;
    int i = 0;
    while (pos != string::npos) {
        i++;
        pos = str.find_first_not_of(delimiters, pos);
        if (pos == string::npos) {
            break;
        }
        start = pos;
        pos = str.find_first_of(delimiters, pos);
        end = pos == string::npos ? str.size() : pos;
        if ( i-1 == n) {
            if (str.substr(start, end-start) == word) {
                for (int i=start; i<end; i++) {
                    str[i] = toupper(str[i]);
                }
            }
            else {
                str = str.substr(0, start) + word + " " + str.substr(start);
            }
            return;
        }
        start = end;
    }
    str += " " + word;
    return; 
}


int main() {
    string str, word;
    int n;


    cout << "Enter a string: ";
    getline(cin, str);
    
    cout << "Capitalaize first and last letters of words:" << endl;
    capitalize_first_last(str);
    cout << str << endl << endl;


    cout << "Enter a word number to delate (1-based): ";
    cin >> n;
    if (!delete_word(str, n)) {
        cout << "The string is empty (or no such word)" << endl;
    }
    cout << "Now the string is:" << endl;
    cout << str << endl << endl;

    cout << "Enter a word to insert: ";
    cin >> word;
    cout << "Insert after which word: ";
    cin >> n;
    insert_word(str, n, word);
    cout << "Now the string is:" << endl;
    cout << str << endl << endl;

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS