Question

In: Computer Science

C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...

C++

Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the loop

Most words in the English language are based on words from ancient Greek and Latin. Breaking down a word and understanding its root can really help in understanding the meaning of more complex words. A root can be at the beginning, middle, or end of a word. A root of a word has no prefix or suffix – think of it as the most basic part of a word. Common prefix and suffix added to roots are things like:

  • Prefixes: un, anti making words like unclear, antibody
  • Suffixes: able, or tion, making words like solvable or solution.
  • There are others

Find the root of a word, so there is a better chance of understanding the meaning of the word.

Help the user find the root of a word by removing the affixes (both prefixes and suffixes). Limit the number of prefixes and suffixes to just 3 each that will be removed. Enter those from the user rather than hard-coding them.

Once the six affixes are supplied, the program should let the user enter in a sentence and get rid of all of the prefixes and suffixes to show what the sentence would be like in the most basic form. Words can be no longer than 20 characters and sentences no longer than 200 characters. Work word by word or process the entire sentence all at once.

    1. Sentences may have capital letter. A capitalized word or one all in upper case should be handled the same as one with lower case letters. Maybe write a function to turn everything read in into the same case to avoid long conditionals.
    1. Realize that sentence may have punctuation. These will not be part of the root of a word and should be removed.
  • read into an array of characters.
  • prompt the user for any input requested. Make sure it is clear from your prompts what the user is expected to do.
  • Use the cstring library with strlen to know when to stop processing the array of characters. strlen should not be in the conditional expression. Consider using it prior to a loop and storing the results of strlen in a variable.
  • Don’t use any global variables in this program!
  • Don’t use the string class – instead use arrays of characters with cstrings
  • Make sure to use C++’s I/O (iostream library) for your input and output.
  • functions must be written

Solutions

Expert Solution

PROGRAM

//Program to remove prefixes and suffixes from words

//include header files

#include<iostream>

#include<cstring>

//define words size

#define WORDSIZE 20

//define size of sentence

#define SENTENCESIZE 200

//use std name space

using namespace std;

//function prototype declarations

//function that get the prefixes

void getPrefixes(char [3][20]);

//function that get the suffixes

void getSuffixes(char [3][20]);

//function that get the sentence

void getSentence(char [200]);

//function that convert the sentence to all uppercase

void convToUpper(char [200]);

//function that deletes a substring from the sentence

void deleteWord(char [], char [], int );

// main function

int main(void)

{

    //variable declarations

    char    suffixes[3][WORDSIZE], //holds suffixes

            prefixes[3][WORDSIZE], //holds prefixes

            sentence[SENTENCESIZE]; //holds sentence

    int index, //holds index of the word for deletion

        j; //loop control variable

    //get senetence

    getSentence(sentence);

    //get prefixes

    getPrefixes(prefixes);

    //get suffixes

    getSuffixes(suffixes);

    //display the senetence

    cout<<endl<<"Your Sentence is: "<<sentence;

    //search prefixes for removal

    for(j=0;j<3;j++)

    {

        index=strstr(sentence,prefixes[j])-sentence;

        if(index>=0)

            deleteWord(sentence,prefixes[j],index);

    }

    //search sufixes for removal

    for(j=0;j<3;j++)

    {

        index=strstr(sentence,suffixes[j])-sentence;

        if(index>=0)

            deleteWord(sentence,suffixes[j],index);

    }

    cout<<endl<<endl<<"The new Sentence is: "<<sentence;

    return 0;

}

//function to get the prefixes

//input: prefix strings

//output: None

//Side effect: prefix string will be returned to the calling function

//

void getPrefixes(char prefixes[3][20])

{

    cout<<endl<<"Enter THREE prefixes you want to remove : "<<endl;

    for(int i=0;i<3;i++)

    {

        cout<<"Enter Prefix #"<<i+1<<" :";

        cin>>prefixes[i];

        cin.ignore(1);

        convToUpper(prefixes[i]);

    }

    return;

}

//function to get the suffixes

//input: suffix strings

//output: None

//Side effect: suffix string will be returned to the calling function

//

void getSuffixes(char suffixes[3][20])

{

    cout<<endl<<"Enter THREE suffixes you want to remove : "<<endl;

   for(int i=0;i<3;i++)

    {

        cout<<"Enter Suffix #"<<i+1<<" :";

        cin>>suffixes[i];

        cin.ignore(1);

        convToUpper(suffixes[i]);

    }

    return;

}

//function to get the senetence

//input: sentence string

//output: None

//Side effect: sentence string will be returned to the calling function

//

void getSentence(char sentence[])

{

    cout<<endl<<"Enter Your SENTENCE : ";

    gets(sentence);

    convToUpper(sentence);

    return;

}

//function to convert the sentence to all uppercase

//input: sentence strings

//output: None

//Side effect: converted sentence string will be returned to the calling function

//

void convToUpper(char sentence[])

{

    for(int i=0;i!='\0';i++)

        sentence[i]=toupper(sentence[i]);

    return;

}

//function to delete a substring from the sentence

//input: sentence and substring strings with index of substring

//output: None

//Side effect: the sentence after deleting the substring

//             string will be returned to the calling function

//

void deleteWord(char str[], char word[], int index)

{

    int i, l;

    /* finding length of word */

    for (l = 0; word[l] != '\0'; l++);

    l--;

    for (i = index; str[i] != '\0'; i++)

    {

        str[i] = str[i + l + 1];

    }

    return;

}

PROGRAM SCREENSHOT

PROGRAM OUTPUT

Enter Your SENTENCE : The words such as dislike, unpopular, and antibody cotrain prefixex and the words like auctioneer, teacher, and retirement contain suffixes.

Enter THREE prefixes you want to remove :

Enter Prefix #1 :un

Enter Prefix #2 :dis

Enter Prefix #3 :anti

Enter THREE suffixes you want to remove :

Enter Suffix #1 :eer

Enter Suffix #2 :er

Enter Suffix #3 :ment

Your Sentence is: The words such as dislike, unpopular, and antibody cotrain prefixex and the words like auctioneer, teacher, and retirement contain suffixes.

The new Sentence is: The words such as like, popular, and body cotrain prefixex and the words like auction, teach, and retire contain suffixes.

PROGRAM OUTPUT SCREENSHOT


Related Solutions

Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters,...
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters, dimes, nickels, and pennies in a given amount of money. The input should be a floating-point value representing a decimal value. Example: 63.87 à 63 [dollars and 87 cents] should output 63 dollars, 3 quarters, 1 dime, 0 nickels, and 2 pennies. 2. In trigonometry the cosine function can be calculated using cos(x) = 1 – x 2 /2! + x 4 /4!- x...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT