In: Computer Science
(Use the String Class to Solve This (and only the iostream, string and cctype libraries))
Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Thus, the input sentence
See an adviser, talk to him, and listen to him.
should produce the following suggested changed version of the sentence:
See an adviser, talk to her or him, and listen to her or him.
Be sure to preserve uppercase letters for the first word of the sentence. The pronoun “his” can be replaced by “her(s) or his”; your program need not decide between “her” and “hers”. Allow the user to repeat this for more sentences until the user says she or he is done.
You need to take care of the following string replacements:
he/she -> she or he
him/her -> her or him
his/her(s) -> her(s) or his
#include <iostream>
#include <stdio.h>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
int main()
{
char ans='y';
while(1)
{
string str1,str2;
cout << "Enter a string: ";
getline(cin,str1);
int len = str1.length();
for(int j=0;j<len;j++)
str1[j]=tolower(str1[j]);
if(len <= 3 && len >0)
{
cout << "Length of string is
too short!!!!" << endl;
for(int k=0;k<len;k++)
str2 +=
str1[k];
};
for(int i=0;i<len && len>3;i++)
if((str1[i]=='h') &&
(str1[i+1]!='\n' && str1[i+2]!='\n'))
{
if(i==0)
{
string s = str1.substr(i,4);
if((s[1]=='i'&&s[2]=='m')||(s[1]=='e'&&s[2]=='r'))
if(s[3]==' ')
{
str2 += "her or him";
i+=2;
}
}
else
{
string s = str1.substr(i-1,5);
if((s[2]=='i'&&s[3]=='m')||(s[2]=='e'&&s[3]=='r'))
if(s[0]==' ' &&
s[0]==' ')
{
str2 +=
"her or him";
i+=2;
}
if((s[2]=='i'&&s[3]=='s'))
if(s[0]==' ' &&
s[0]==' ')
{
str2 +=
"her(s) or his";
i+=2;
}
if((s[2]=='e'))
if(s[0]==' ' &&
s[0]==' ')
{
str2 +=
"she or he";
i+=1;
}
};
}
else str2 += str1[i];
str2[0]=toupper(str2[0]);
cout << str2 << endl;
cout << "Again (Y/N) ?";
cin >> ans;
cin.ignore();
if(ans=='n'|| ans=='N') break;
}
return 0;
}