Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #!/usr/bin/ruby -w
  2. #Franklin Aquino
  3. #Scripting languges assignment
  4. #Asignment 12
  5. #Ruby Asignment using String class
  6.  
  7. #method to check if palindrome
  8. def palindrome?(str)
  9. str == str.reverse
  10. end
  11.  
  12. puts "Enter First String\n"
  13. $firstString = String.new()
  14. $secondString = String.new()
  15. $combinedString = String.new()
  16. $firstString = gets.chomp
  17. #Gets string length of entered string
  18. $stringLength = $firstString.length
  19. puts "Length of [" + $firstString + "] is " + $stringLength.to_s
  20. #Display string backwards
  21. puts "Entered string reveresed! " + $firstString.reverse
  22. #Count number of words in string
  23. $stringArray = $firstString.split()
  24. puts "Number of words in string is " + $stringArray.length.to_s
  25. #Ask for second string
  26. puts "Enter second String"
  27. $secondString = gets.chomp
  28. puts "Entered second string :" + $secondString
  29. #Add second string to First
  30. $combinedString = $firstString.dup
  31. puts "The two strings added together: " + $combinedString.concat($secondString)
  32. puts $firstString
  33. #Compare first string to second string
  34. $stringTest = $firstString <=> $secondString
  35. if $stringTest == 0
  36. puts "Two Strings are equal"
  37. else
  38. puts "Strings are not equal"
  39. end
  40. #Checks if first string entered is palindrome
  41. $paliTest = palindrome?($firstString)
  42. if $paliTest == TRUE
  43. puts "First string is palindrome"
  44. else
  45. puts "First string is not palindrome"
  46. end
  47. #extarct substring of first 5 characters
  48. $firstString = $firstString[0..5]
  49. puts "First five characters of first string: " + $firstString
  50. #REverse the string again
  51. puts "Substring reveresed: " + $firstString.reverse
  52. #Convert to upper and lower first string
  53. 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