Ruby program to remove common characters from two strings
class RemoveCommonCharacters
def removeCommonCharacter
print "Enter the first string: "
first_string = gets.chomp.downcase
new_first = first_string
print "Enter the second string: "
second_string = gets.chomp.downcase
new_second = second_string
char_array = first_string.split("")
char_array.each do |v|
unless v == " "
if second_string.include?v
new_first = new_first.gsub(v, "")
new_second = new_second.gsub(v, "")
end
end
end
puts "Original strings"
puts "First string: #{first_string}"
puts "Second string: #{second_string}"
puts "After removing common characters"
puts "First string: #{new_first}"
puts "Second string: #{new_second}"
end
end
p = RemoveCommonCharacters.new
p.removeCommonCharacter
Output:
class RemoveCommonCharacters
def removeCommonCharacter
print "Enter the first string: "
first_string = gets.chomp.downcase
new_first = first_string
print "Enter the second string: "
second_string = gets.chomp.downcase
new_second = second_string
char_array = first_string.split("")
char_array.each do |v|
unless v == " "
if second_string.include?v
new_first = new_first.gsub(v, "")
new_second = new_second.gsub(v, "")
end
end
end
puts "Original strings"
puts "First string: #{first_string}"
puts "Second string: #{second_string}"
puts "After removing common characters"
puts "First string: #{new_first}"
puts "Second string: #{new_second}"
end
end
p = RemoveCommonCharacters.new
p.removeCommonCharacter
Output:
Enter the first string: umesh kumar kushwaha
Enter the second string: rajesh singh
Original strings
First string: umesh kumar kushwaha
Second string: rajesh singh
After removing common characters
First string: um kum kuw
Second string: j ing
|
No comments:
Post a Comment