In: Computer Science
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:
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.
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