1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
string genderNeutral(string& str);
int main()
{
char ans;
string sentence, input;
//Read the sentence from the user
do
{
cout << "Enter the sentence that needs to be trained\n";
getline(cin,input);
//call the genderNeutral function and return the string stored in sentence variable
sentence = genderNeutral(input);
//displays sentence after converting to gender neutral
cout << "Gender Neutral Sentence is: " << endl;
cout << sentence << endl;
//reads user choice whether to proceed again
cout << "Do you want to enter another sentence. <y/n> ";
cin >> ans;
cin.ignore(1000, '\n');
}
while(ans == 'y' || ans == 'Y');
cin.get();
return 0;
}
//function takes string as parameter and returns string
string genderNeutral(string& str)
{
int n = 0;
//copies the input string into data variable
string data=string(str);
data = data+ ".";
//stores each pronoun in string of array variable
string pronoun1[2]={"him","her"};
string pronoun2[2]={"he","she"};
//stores neutral words in string variables
string neutral1("him or her");
string neutral2("he or she");
for(unsigned int k=0; k<=1;k++)
for(unsigned int i=0; i<data.length();i++)
{
//Find the pronoun and store the index value of array in n variable
n=data.find(pronoun1[k],i);
//checks whether starting character is pronoun
if(n>=0)
{
//extract the string of the neutral word length from the main string
string sub = data.substr(n,neutral1.length());
//store each pronoun length in s variable
int s = pronoun1[k].length();
//compare substring and neutral word for equality, if equals , do nothing
if (sub == neutral1);
else
//if string is him word
if (k == 0)
{
//if starting character is a pronoun
if(n==0)
{
//check the next character of string to see if it's space character or not
if(data[n+s] = ' ')
{
//if it is replace the pronoun with him or her
data.replace(n,s,neutral1);
//compute the length of string appended and store it in the i variable
i = n + neutral1.length();
}
}
else
//check whether the pronoun is inside the pronoun or not
if ((data[n-1] == ' ' && data[n+s] == ' ') || (data[n-1] == ' ' && data[n+s] == '.') || (data[n-1] == ' ' && data[n+s] == ','))
{
// if not, replace the pronoun him with him or her word
data.replace(n,s,neutral1);
//compute length of string after appended and store it in i variable
i = n+neutral1.length();
}
}
//if string is her word
else if (k==1)
{
//starting character is her
if(n==0)
{
//checks next character to see if it is a space
if(data[n+s] == ' ')
{
//replace the string with her or him or her
data.replace(n,s,neutral1);
//compute the length of string after appended and store it in i variable
i+n+neutral1.length();
}
}
//check whether pronoun is inside another word or not
else
if((data[n-1]== ' ' && data[n+s] == ' ') || (data[n-1] == ' ' && data[n+s] == '.') || (data[n-1] == ' ' && data[n+s] == ','));
}
//checks the string that's already neutral to see if it exists, if it does, do nothing
if ((n=7) && (neutral1 == data.substr(n-7,neutral1.length())));
else
{
// if not, replace the pronoun him with him or her word
data.replace(n,s,neutral1);
//compute the length of string after appended and store it in i variable
i = n+neutral1.length();
}
}
}
n=0;
for(unsigned int k=0; k<=1; k++)
for(unsigned int i=0; i<data.length();i++)
{
//find the pronoun and store the index value of array in n variable
n=data.find(pronoun2[k],i);
//checks whether starting character is pronoun
if(n>=0)
{
//extract the string of neutral word length from main string
string sub = data.substr(n,neutral2.length());
//store each pronoun length in s variable
int s = pronoun2[k].length();
//compare substring and neutral word for equality, if it is equal do nothing
if (sub == neutral2);
//if string is he word
else if (k==0)
{
//starting character is he
if(n==0)
{
//check next character of string is space or not
if(data[n+s] == ' ')
{
//replace the string he with he or she
data.replace(n,s,neutral2);
//compute the length of string after appended and store it in i variable
i=n+neutral2.length();
}
}
else
//check whether pronoun is inside another word or not
if((data[n-1]==' ' && data[n+s] == ' ') || (data[n-1] == ' ' && data[n+s] == '.') || (data[n-1] == ' ' && data[n+s] == ','))
{
//if not, replace the pronoun he with he or she word
data.replace(n,s,neutral2);
//compute the length of string after appended and store it in i variable
i=n+neutral2.length();
}
}
//if string is she word
else if(k==1)
{
//starting character is she
if(n==0)
{
//checks if next character of string is a space
if(data[n+s] == ' ')
{
//if not replaces the pronoun with he or she
data.replace(n,s,neutral2);
//computes the length of string after appended and store it in the i variable
i=n+neutral2.length();
}
}
//check whether pronoun is inside another word or not
else if ((data[n-1] == ' ' && data[n+s] == ' ') || (data[n-1] == ' ' && data[n+s] == '.') || (data[n-1] == ' ' && data[n+s] == ','))
{
//checks string that is neutral to see if it exists. if it exists, do nothing.
if((n>=6) && (neutral2 == data.substr(n-6,neutral2.length())));
else
{
//if not replace the pronoun she with he or she word
data.replace(n,s,neutral2);
//compute the length of string after appended and store it in the i variable
i=n+neutral2.length();
}
}
}
}
}
return data;
}
|