Change gender words in a given string
Here given code implementation process.
import java.util.HashMap;
/*
Java program
Change gender words in a given string
*/
public class Replace
{
public boolean specialSymbol(char data)
{
if (data >= 'a' && data <= 'z' || (data >= 'A' && data <= 'Z'))
{
return false;
}
return true;
}
public String replaceGender(String text)
{
if (text.length() == 0)
{
return text;
}
// Collect all words of string
String[] words = text.split(" ");
HashMap < String, String > record =
new HashMap < String, String > ();
record.put("mother", "father");
record.put("uncle", "aunt");
record.put("aunt", "uncle");
record.put("boy", "girl");
record.put("girl", "boy");
record.put("father", "mother");
record.put("son", "daughter");
record.put("daughter", "son");
record.put("husband", "wife");
record.put("wife", "husband");
record.put("mr", "ms");
record.put("ms", "mr");
record.put("he", "she");
record.put("she", "he");
record.put("male", "female");
record.put("female", "male");
record.put("man", "woman");
record.put("woman", "man");
record.put("sir", "madam");
record.put("madam", "sir");
String result = "";
String auxiliary = "";
for (int i = 0; i < words.length; ++i)
{
if (i != 0)
{
result += " ";
}
// Avoid lowercase and uppercase and camel case word
auxiliary = words[i].toLowerCase();
if (record.containsKey(auxiliary))
{
result += record.get(auxiliary);
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
String left = "";
String right = "";
String middle = "";
int l = 0;
int r = auxiliary.length() - 1;
// Trim the special characters at beginning
while (l < auxiliary.length() &&
specialSymbol(auxiliary.charAt(l)))
{
left += auxiliary.charAt(l);
l++;
}
// Trim with special characters at ends
while (r >= 0 && specialSymbol(auxiliary.charAt(r)))
{
right += auxiliary.charAt(r);
r--;
}
if (l == auxiliary.length())
{
// Word containing special characters
result += words[i];
}
else
{
while (l <= r)
{
middle += auxiliary.charAt(l);
l++;
}
if (record.containsKey(middle))
{
result += left + record.get(middle) + right;
}
else
{
result += words[i];
}
}
}
}
return result;
}
public static void main(String[] args)
{
Replace task = new Replace();
String text = "This is a boy not a girl, Invite your [uncle] and son.";
System.out.println(" Before Text : " + text);
text = task.replaceGender(text);
System.out.println(" After Text : " + text);
}
}
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
// Include header file
#include <iostream>
#include <string.h>
#include <unordered_map>
#include <algorithm>
using namespace std;
/*
C++ program
Change gender words in a given string
*/
char asciitolower(char in )
{
if ( in <= 'Z' && in >= 'A')
{
return in-('Z' - 'z');
}
return in;
}
class Replace
{
public: bool specialSymbol(char data)
{
if (data >= 'a' && data <= 'z' ||
(data >= 'A' && data <= 'Z'))
{
return false;
}
return true;
}
string replaceGender(char *text)
{
// Collect all words of string
char *words = strtok(text, " ");
unordered_map < string, string > record;
record["mother"] = "father";
record["uncle"] = "aunt";
record["aunt"] = "uncle";
record["boy"] = "girl";
record["girl"] = "boy";
record["father"] = "mother";
record["son"] = "daughter";
record["daughter"] = "son";
record["husband"] = "wife";
record["wife"] = "husband";
record["mr"] = "ms";
record["ms"] = "mr";
record["he"] = "she";
record["she"] = "he";
record["male"] = "female";
record["female"] = "male";
record["man"] = "woman";
record["woman"] = "man";
record["sir"] = "madam";
record["madam"] = "sir";
string result = "";
string auxiliary = "";
bool i = true;
while (words != NULL)
{
if (i != false)
{
result += " ";
i = true;
}
// Avoid lowercase and uppercase and camel case word
auxiliary = words;
transform(auxiliary.begin(),
auxiliary.end(),
auxiliary.begin(),
asciitolower);
if (record.find(auxiliary) != record.end())
{
result += record[auxiliary];
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
string left = "";
string right = "";
string middle = "";
int l = 0;
int r = auxiliary.length() - 1;
// Trim the special characters at beginning
while (l < auxiliary.length() &&
this->specialSymbol(auxiliary[l]))
{
left += auxiliary[l];
l++;
}
// Trim with special characters at ends
while (r >= 0 && this->specialSymbol(auxiliary[r]))
{
right += auxiliary[r];
r--;
}
if (l == auxiliary.length())
{
// Word containing special characters
result += words;
}
else
{
while (l <= r)
{
middle += auxiliary[l];
l++;
}
if (record.find(middle) != record.end())
{
result += left + record[middle] + right;
}
else
{
result += words;
}
}
}
words = strtok(NULL, " ");
}
return result;
}
};
int main()
{
Replace *task = new Replace();
char text[] = "This is a boy not a girl, Invite your [uncle] and son.";
string r = text;
cout << " Before Text : " << r << endl;
r = task->replaceGender(text);
cout << " After Text : " << r << endl;
return 0;
}
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp program
Change gender words in a given string
*/
public class Replace
{
public Boolean specialSymbol(char data)
{
if (data >= 'a' && data <= 'z' ||
(data >= 'A' && data <= 'Z'))
{
return false;
}
return true;
}
public String replaceGender(String text)
{
if (text.Length == 0)
{
return text;
}
// Collect all words of string
String[] words = text.Split(" ");
Dictionary < string, string > record =
new Dictionary < string, string > ();
record.Add("mother", "father");
record.Add("uncle", "aunt");
record.Add("aunt", "uncle");
record.Add("boy", "girl");
record.Add("girl", "boy");
record.Add("father", "mother");
record.Add("son", "daughter");
record.Add("daughter", "son");
record.Add("husband", "wife");
record.Add("wife", "husband");
record.Add("mr", "ms");
record.Add("ms", "mr");
record.Add("he", "she");
record.Add("she", "he");
record.Add("male", "female");
record.Add("female", "male");
record.Add("man", "woman");
record.Add("woman", "man");
record.Add("sir", "madam");
record.Add("madam", "sir");
String result = "";
String auxiliary = "";
for (int i = 0; i < words.Length; ++i)
{
if (i != 0)
{
result += " ";
}
// Avoid lowercase and uppercase and camel case word
auxiliary = words[i].ToLower();
if (record.ContainsKey(auxiliary))
{
result += record[auxiliary];
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
String left = "";
String right = "";
String middle = "";
int l = 0;
int r = auxiliary.Length - 1;
// Trim the special characters at beginning
while (l < auxiliary.Length && this.specialSymbol(auxiliary[l]))
{
left += auxiliary[l];
l++;
}
// Trim with special characters at ends
while (r >= 0 && this.specialSymbol(auxiliary[r]))
{
right += auxiliary[r];
r--;
}
if (l == auxiliary.Length)
{
// Word containing special characters
result += words[i];
}
else
{
while (l <= r)
{
middle += auxiliary[l];
l++;
}
if (record.ContainsKey(middle))
{
result += left + record[middle] + right;
}
else
{
result += words[i];
}
}
}
}
return result;
}
public static void Main(String[] args)
{
Replace task = new Replace();
String text = "This is a boy not a girl, Invite your [uncle] and son.";
Console.WriteLine(" Before Text : " + text);
text = task.replaceGender(text);
Console.WriteLine(" After Text : " + text);
}
}
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
<?php
/*
Php program
Change gender words in a given string
*/
class Replace
{
public function specialSymbol($data)
{
if ($data >= 'a' && $data <= 'z'
|| ($data >= 'A' && $data <= 'Z'))
{
return false;
}
return true;
}
public function replaceGender($text)
{
if (strlen($text) == 0)
{
return $text;
}
// Collect all words of string
$words = explode(" ", $text);
$record = array();
$record["mother"] = "father";
$record["uncle"] = "aunt";
$record["aunt"] = "uncle";
$record["boy"] = "girl";
$record["girl"] = "boy";
$record["father"] = "mother";
$record["son"] = "daughter";
$record["daughter"] = "son";
$record["husband"] = "wife";
$record["wife"] = "husband";
$record["mr"] = "ms";
$record["ms"] = "mr";
$record["he"] = "she";
$record["she"] = "he";
$record["male"] = "female";
$record["female"] = "male";
$record["man"] = "woman";
$record["woman"] = "man";
$record["sir"] = "madam";
$record["madam"] = "sir";
$result = "";
$auxiliary = "";
for ($i = 0; $i < count($words); ++$i)
{
if ($i != 0)
{
$result .= " ";
}
// Avoid lowercase and uppercase and camel case word
$auxiliary = strtolower($words[$i]);
if (array_key_exists($auxiliary, $record))
{
$result .= $record[$auxiliary];
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
$left = "";
$right = "";
$middle = "";
$l = 0;
$r = strlen($auxiliary) - 1;
// Trim the special characters at beginning
while ($l < strlen($auxiliary) &&
$this->specialSymbol($auxiliary[$l]))
{
$left .= $auxiliary[$l];
$l++;
}
// Trim with special characters at ends
while ($r >= 0 && $this->specialSymbol($auxiliary[$r]))
{
$right .= $auxiliary[$r];
$r--;
}
if ($l == strlen($auxiliary))
{
// Word containing special characters
$result .= $words[$i];
}
else
{
while ($l <= $r)
{
$middle .= $auxiliary[$l];
$l++;
}
if (array_key_exists($middle, $record))
{
$result .= $left.$record[$middle].$right;
}
else
{
$result .= $words[$i];
}
}
}
}
return $result;
}
}
function main()
{
$task = new Replace();
$text = "This is a boy not a girl, Invite your [uncle] and son.";
echo(" Before Text : ".$text."\n");
$text = $task->replaceGender($text);
echo(" After Text : ".$text."\n");
}
main();
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
package main
import "strings"
import "fmt"
/*
Go program
Change gender words in a given string
*/
func specialSymbol(data byte) bool {
if data >= 'a' && data <= 'z' || (data >= 'A' && data <= 'Z') {
return false
}
return true
}
func replaceGender(text string) string {
if len(text) == 0 {
return text
}
// Collect all words of string
var words = strings.Split(text, " ")
var record = make(map[string] string)
record["mother"] = "father"
record["uncle"] = "aunt"
record["aunt"] = "uncle"
record["boy"] = "girl"
record["girl"] = "boy"
record["father"] = "mother"
record["son"] = "daughter"
record["daughter"] = "son"
record["husband"] = "wife"
record["wife"] = "husband"
record["mr"] = "ms"
record["ms"] = "mr"
record["he"] = "she"
record["she"] = "he"
record["male"] = "female"
record["female"] = "male"
record["man"] = "woman"
record["woman"] = "man"
record["sir"] = "madam"
record["madam"] = "sir"
var result string = ""
var auxiliary string = ""
for i := 0 ; i < len(words) ; i++ {
if i != 0 {
result += " "
}
// Avoid lowercase and uppercase and camel case word
auxiliary = strings.ToLower(words[i])
if _, found := record[auxiliary] ; found {
result += record[auxiliary]
} else {
// In case gender word start and end with special characters
// Declare some useful variables
var left string = ""
var right string = ""
var middle string = ""
var l int = 0
var r int = len(auxiliary) - 1
// Trim the special characters at beginning
for (l < len(auxiliary) && specialSymbol(auxiliary[l])) {
left += string(auxiliary[l])
l++
}
// Trim with special characters at ends
for (r >= 0 && specialSymbol(auxiliary[r])) {
right += string(auxiliary[r])
r--
}
if l == len(auxiliary) {
// Word containing special characters
result += string(words[i])
} else {
for (l <= r) {
middle += string(auxiliary[l])
l++
}
if _, found := record[middle] ; found {
result += left + record[middle] + right
} else {
result += words[i]
}
}
}
}
return result
}
func main() {
var text string = "This is a boy not a girl, Invite your [uncle] and son."
fmt.Println(" Before Text : ", text)
text = replaceGender(text)
fmt.Println(" After Text : ", text)
}
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
/*
Node JS program
Change gender words in a given string
*/
class Replace
{
specialSymbol(data)
{
if (data >= 'a' && data <= 'z' ||
(data >= 'A' && data <= 'Z'))
{
return false;
}
return true;
}
replaceGender(text)
{
if (text.length == 0)
{
return text;
}
// Collect all words of string
var words = text.split(" ");
var record = new Map();
record.set("mother", "father");
record.set("uncle", "aunt");
record.set("aunt", "uncle");
record.set("boy", "girl");
record.set("girl", "boy");
record.set("father", "mother");
record.set("son", "daughter");
record.set("daughter", "son");
record.set("husband", "wife");
record.set("wife", "husband");
record.set("mr", "ms");
record.set("ms", "mr");
record.set("he", "she");
record.set("she", "he");
record.set("male", "female");
record.set("female", "male");
record.set("man", "woman");
record.set("woman", "man");
record.set("sir", "madam");
record.set("madam", "sir");
var result = "";
var auxiliary = "";
for (var i = 0; i < words.length; ++i)
{
if (i != 0)
{
result += " ";
}
// Avoid lowercase and uppercase and camel case word
auxiliary = words[i].toLowerCase();
if (record.has(auxiliary))
{
result += record.get(auxiliary);
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
var left = "";
var right = "";
var middle = "";
var l = 0;
var r = auxiliary.length - 1;
// Trim the special characters at beginning
while (l < auxiliary.length &&
this.specialSymbol(auxiliary.charAt(l)))
{
left += auxiliary.charAt(l);
l++;
}
// Trim with special characters at ends
while (r >= 0 && this.specialSymbol(auxiliary.charAt(r)))
{
right += auxiliary.charAt(r);
r--;
}
if (l == auxiliary.length)
{
// Word containing special characters
result += words[i];
}
else
{
while (l <= r)
{
middle += auxiliary.charAt(l);
l++;
}
if (record.has(middle))
{
result += left + record.get(middle) + right;
}
else
{
result += words[i];
}
}
}
}
return result;
}
}
function main()
{
var task = new Replace();
var text = "This is a boy not a girl, Invite your [uncle] and son.";
console.log(" Before Text : " + text);
text = task.replaceGender(text);
console.log(" After Text : " + text);
}
main();
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
# Python 3 program
# Change gender words in a given string
class Replace :
def specialSymbol(self, data) :
if (data >= 'a'
and data <= 'z'
or(data >= 'A'
and data <= 'Z')) :
return False
return True
def replaceGender(self, text) :
if (len(text) == 0) :
return text
# Collect all words of string
words = text.split(" ")
record = dict()
record["mother"] = "father"
record["uncle"] = "aunt"
record["aunt"] = "uncle"
record["boy"] = "girl"
record["girl"] = "boy"
record["father"] = "mother"
record["son"] = "daughter"
record["daughter"] = "son"
record["husband"] = "wife"
record["wife"] = "husband"
record["mr"] = "ms"
record["ms"] = "mr"
record["he"] = "she"
record["she"] = "he"
record["male"] = "female"
record["female"] = "male"
record["man"] = "woman"
record["woman"] = "man"
record["sir"] = "madam"
record["madam"] = "sir"
result = ""
auxiliary = ""
i = 0
while (i < len(words)) :
if (i != 0) :
result += " "
# Avoid lowercase and uppercase and camel case word
auxiliary = words[i].lower()
if ((auxiliary in record.keys())) :
result += record.get(auxiliary)
else :
# In case gender word start and end with special characters
# Declare some useful variables
left = ""
right = ""
middle = ""
l = 0
r = len(auxiliary) - 1
# Trim the special characters at beginning
while (l < len(auxiliary) and self.specialSymbol(auxiliary[l])) :
left += auxiliary[l]
l += 1
# Trim with special characters at ends
while (r >= 0 and self.specialSymbol(auxiliary[r])) :
right += auxiliary[r]
r -= 1
if (l == len(auxiliary)) :
# Word containing special characters
result += words[i]
else :
while (l <= r) :
middle += auxiliary[l]
l += 1
if ((middle in record.keys())) :
result += left + record.get(middle) + right
else :
result += words[i]
i += 1
return result
def main() :
task = Replace()
text = "This is a boy not a girl, Invite your [uncle] and son."
print(" Before Text : ", text)
text = task.replaceGender(text)
print(" After Text : ", text)
if __name__ == "__main__": main()
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
# Ruby program
# Change gender words in a given string
class Replace
def specialSymbol(data)
if (data >= 'a' && data <= 'z' || (data >= 'A' && data <= 'Z'))
return false
end
return true
end
def replaceGender(text)
if (text.length == 0)
return text
end
# Collect all words of string
words = text.split(" ")
record = Hash.new()
record["mother"] = "father"
record["uncle"] = "aunt"
record["aunt"] = "uncle"
record["boy"] = "girl"
record["girl"] = "boy"
record["father"] = "mother"
record["son"] = "daughter"
record["daughter"] = "son"
record["husband"] = "wife"
record["wife"] = "husband"
record["mr"] = "ms"
record["ms"] = "mr"
record["he"] = "she"
record["she"] = "he"
record["male"] = "female"
record["female"] = "male"
record["man"] = "woman"
record["woman"] = "man"
record["sir"] = "madam"
record["madam"] = "sir"
result = ""
auxiliary = ""
i = 0
while (i < words.length)
if (i != 0)
result += " "
end
# Avoid lowercase and uppercase and camel case word
auxiliary = words[i].downcase
if (record.key?(auxiliary))
result += record[auxiliary]
else
# In case gender word start and end with special characters
# Declare some useful variables
left = ""
right = ""
middle = ""
l = 0
r = auxiliary.length - 1
# Trim the special characters at beginning
while (l < auxiliary.length && self.specialSymbol(auxiliary[l]))
left += auxiliary[l]
l += 1
end
# Trim with special characters at ends
while (r >= 0 && self.specialSymbol(auxiliary[r]))
right += auxiliary[r]
r -= 1
end
if (l == auxiliary.length)
# Word containing special characters
result += words[i]
else
while (l <= r)
middle += auxiliary[l]
l += 1
end
if (record.key?(middle))
result += left + record[middle] + right
else
result += words[i]
end
end
end
i += 1
end
return result
end
end
def main()
task = Replace.new()
text = "This is a boy not a girl, Invite your [uncle] and son."
print(" Before Text : ", text, "\n")
text = task.replaceGender(text)
print(" After Text : ", text, "\n")
end
main()
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
import scala.collection.mutable._;
/*
Scala program
Change gender words in a given string
*/
class Replace()
{
def specialSymbol(data: Char): Boolean = {
if (data >= 'a' && data <= 'z' ||
(data >= 'A' && data <= 'Z'))
{
return false;
}
return true;
}
def replaceGender(text: String): String = {
if (text.length() == 0)
{
return text;
}
// Collect all words of string
var words: Array[String] = text.split(" ");
var record: HashMap[String, String] =
new HashMap[String, String]();
record.addOne("mother", "father");
record.addOne("uncle", "aunt");
record.addOne("aunt", "uncle");
record.addOne("boy", "girl");
record.addOne("girl", "boy");
record.addOne("father", "mother");
record.addOne("son", "daughter");
record.addOne("daughter", "son");
record.addOne("husband", "wife");
record.addOne("wife", "husband");
record.addOne("mr", "ms");
record.addOne("ms", "mr");
record.addOne("he", "she");
record.addOne("she", "he");
record.addOne("male", "female");
record.addOne("female", "male");
record.addOne("man", "woman");
record.addOne("woman", "man");
record.addOne("sir", "madam");
record.addOne("madam", "sir");
var result: String = "";
var auxiliary: String = "";
var i: Int = 0;
while (i < words.length)
{
if (i != 0)
{
result += " ";
}
// Avoid lowercase and uppercase and camel case word
auxiliary = words(i).toLowerCase();
if (record.contains(auxiliary))
{
result += record.get(auxiliary).get;
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
var left: String = "";
var right: String = "";
var middle: String = "";
var l: Int = 0;
var r: Int = auxiliary.length() - 1;
// Trim the special characters at beginning
while (l < auxiliary.length() &&
specialSymbol(auxiliary.charAt(l)))
{
left += auxiliary.charAt(l);
l += 1;
}
// Trim with special characters at ends
while (r >= 0 && specialSymbol(auxiliary.charAt(r)))
{
right += auxiliary.charAt(r);
r -= 1;
}
if (l == auxiliary.length())
{
// Word containing special characters
result += words(i);
}
else
{
while (l <= r)
{
middle += auxiliary.charAt(l);
l += 1;
}
if (record.contains(middle))
{
result += left + record.get(middle).get + right;
}
else
{
result += words(i);
}
}
}
i += 1;
}
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Replace = new Replace();
var text: String = "This is a boy not a girl, Invite your [Uncle] and son.";
println(" Before Text : " + text);
text = task.replaceGender(text);
println(" After Text : " + text);
}
}
input
Before Text : This is a boy not a girl, Invite your [Uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
import Foundation;
/*
Swift 4 program
Change gender words in a given string
*/
class Replace
{
func specialSymbol(_ ch: Character) -> Bool
{
let data = UnicodeScalar(String(ch))!.value;
if (data >= 97 && data <= 122 || (data >= 65 && data <= 90))
{
return false;
}
return true;
}
func replaceGender(_ text: String) -> String
{
if (text.count == 0)
{
return text;
}
// Collect all words of string
let words: [String] = text.split
{
$0 == " "
}.map(String.init);
var record = [String : String]();
record["mother"] = "father";
record["uncle"] = "aunt";
record["aunt"] = "uncle";
record["boy"] = "girl";
record["girl"] = "boy";
record["father"] = "mother";
record["son"] = "daughter";
record["daughter"] = "son";
record["husband"] = "wife";
record["wife"] = "husband";
record["mr"] = "ms";
record["ms"] = "mr";
record["he"] = "she";
record["she"] = "he";
record["male"] = "female";
record["female"] = "male";
record["man"] = "woman";
record["woman"] = "man";
record["sir"] = "madam";
record["madam"] = "sir";
var result: String = "";
var current: String = "";
var i: Int = 0;
while (i < words.count)
{
if (i != 0)
{
result += " ";
}
// Avoid lowercase and uppercase and camel case word
current = words[i].lowercased();
if (record.keys.contains(current))
{
result += record[current]!;
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
var left: String = "";
var right: String = "";
var middle: String = "";
var l: Int = 0;
var r: Int = current.count - 1;
var auxiliary = Array(current);
// Trim the special characters at beginning
while (l < auxiliary.count && self.specialSymbol(auxiliary[l]))
{
left += String(auxiliary[l]);
l += 1;
}
// Trim with special characters at ends
while (r >= 0 && self.specialSymbol(auxiliary[r]))
{
right += String(auxiliary[r]);
r -= 1;
}
if (l == auxiliary.count)
{
// Word containing special characters
result += String(words[i]);
}
else
{
while (l <= r)
{
middle += String(auxiliary[l]);
l += 1;
}
if (record.keys.contains(middle))
{
result += left + record[middle]! + right;
}
else
{
result += words[i];
}
}
}
i += 1;
}
return result;
}
}
func main()
{
let task: Replace = Replace();
var text: String = "This is a boy not a girl, Invite your [uncle] and son.";
print(" Before Text : ", text);
text = task.replaceGender(text);
print(" After Text : ", text);
}
main();
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
/*
Kotlin program
Change gender words in a given string
*/
class Replace
{
fun specialSymbol(data: Char): Boolean
{
if (data.toInt() >= 'a'.toInt() && data.toInt() <= 'z'.toInt()
|| (data.toInt() >= 'A'.toInt() && data.toInt() <= 'Z'.toInt())
)
{
return false;
}
return true;
}
fun replaceGender(text: String): String
{
if (text.length == 0)
{
return text;
}
// Collect all words of string
val words = text.split(" ");
var record: HashMap < String , String > =
HashMap < String , String > ();
record.put("mother", "father");
record.put("uncle", "aunt");
record.put("aunt", "uncle");
record.put("boy", "girl");
record.put("girl", "boy");
record.put("father", "mother");
record.put("son", "daughter");
record.put("daughter", "son");
record.put("husband", "wife");
record.put("wife", "husband");
record.put("mr", "ms");
record.put("ms", "mr");
record.put("he", "she");
record.put("she", "he");
record.put("male", "female");
record.put("female", "male");
record.put("man", "woman");
record.put("woman", "man");
record.put("sir", "madam");
record.put("madam", "sir");
var result: String = "";
var auxiliary: String;
var i: Int = 0;
while (i < words.count())
{
if (i != 0)
{
result += " ";
}
// Avoid lowercase and uppercase and camel case word
auxiliary = words[i].toLowerCase();
if (record.containsKey(auxiliary))
{
result += record.getValue(auxiliary);
}
else
{
// In case gender word start and end with special characters
// Declare some useful variables
var left: String = "";
var right: String = "";
var middle: String = "";
var l: Int = 0;
var r: Int = auxiliary.length - 1;
// Trim the special characters at beginning
while (l < auxiliary.length &&
this.specialSymbol(auxiliary.get(l)))
{
left += auxiliary.get(l);
l += 1;
}
// Trim with special characters at ends
while (r >= 0 && this.specialSymbol(auxiliary.get(r)))
{
right += auxiliary.get(r);
r -= 1;
}
if (l == auxiliary.length)
{
// Word containing special characters
result += words[i];
}
else
{
while (l <= r)
{
middle += auxiliary.get(l);
l += 1;
}
if (record.containsKey(middle))
{
result += left + record.getValue(middle) + right;
}
else
{
result += words[i];
}
}
}
i += 1;
}
return result;
}
}
fun main(args: Array < String > ): Unit
{
val task: Replace = Replace();
var text: String = "This is a boy not a girl, Invite your [uncle] and son.";
println(" Before Text : " + text);
text = task.replaceGender(text);
println(" After Text : " + text);
}
input
Before Text : This is a boy not a girl, Invite your [uncle] and son.
After Text : This is a girl not a boy, Invite your [aunt] and daughter.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment