Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby -w
- #Franklin Aquino
- #Scripting languges assignment
- #Asignment 12
- #Ruby Asignment using String class
- #method to check if palindrome
- def palindrome?(str)
- str == str.reverse
- end
- puts "Enter First String\n"
- $firstString = String.new()
- $secondString = String.new()
- $combinedString = String.new()
- $firstString = gets.chomp
- #Gets string length of entered string
- $stringLength = $firstString.length
- puts "Length of [" + $firstString + "] is " + $stringLength.to_s
- #Display string backwards
- puts "Entered string reveresed! " + $firstString.reverse
- #Count number of words in string
- $stringArray = $firstString.split()
- puts "Number of words in string is " + $stringArray.length.to_s
- #Ask for second string
- puts "Enter second String"
- $secondString = gets.chomp
- puts "Entered second string :" + $secondString
- #Add second string to First
- $combinedString = $firstString.dup
- puts "The two strings added together: " + $combinedString.concat($secondString)
- puts $firstString
- #Compare first string to second string
- $stringTest = $firstString <=> $secondString
- if $stringTest == 0
- puts "Two Strings are equal"
- else
- puts "Strings are not equal"
- end
- #Checks if first string entered is palindrome
- $paliTest = palindrome?($firstString)
- if $paliTest == TRUE
- puts "First string is palindrome"
- else
- puts "First string is not palindrome"
- end
- #extarct substring of first 5 characters
- $firstString = $firstString[0..5]
- puts "First five characters of first string: " + $firstString
- #REverse the string again
- puts "Substring reveresed: " + $firstString.reverse
- #Convert to upper and lower first string
- puts "First String in all upper: " + $firstString.upcase + " First String all lower case: " + $firstString.downcase
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement