• Source
    1. #
    2. #Write A Script To Perform Following String Operations Using Menu:
    3. # 1)COMPARE TWO STRINGS.
    4. # 2)JOIN TWO STRINGS.
    5. # 3)FIND THE LENGTH OF A GIVEN STRING.
    6. # 4)OCCURRENCE OF CHARACTER AND WORDS
    7. # 5)REVERSE THE STRING.
    8. #
    9. clear
    10. echo "==================== AVAILABLE CHOICES ========================="
    11. echo "1. Compare two strings"
    12. echo "2. Join two strings"
    13. echo "3. Find length of a given string"
    14. echo "4. Occurance of character and word in a string"
    15. echo "5. Reverse the string"
    16. echo "E. Exit"
    17. echo "================================================================"
    18. echo "Enter your choice :"
    19. read choice
    20. while [ "$choice" != "e" -o "$choice" != "E" ]
    21. do
    22. case $choice in
    23. "1")
    24. echo -n "Enter first string :"
    25. read firststr
    26. echo -n "Enter second string :"
    27. read secondstr
    28. #if [ "$firststr" = "$secondstr" ] #FOR CASE SENSITIVE CHECK
    29. if [ `echo $firststr | grep -ci $secondstr` -gt 0 ] #FOR CASE INSENSITIVE CHECK
    30. then
    31. echo "Both strings are same"
    32. else
    33. echo "Both strings are different"
    34. fi
    35. ;;
    36. "2")
    37. echo -n "Enter first string :"
    38. read firststr
    39. echo -n "Enter second string :"
    40. read secondstr
    41. echo "Joining of two string is : $firststr$secondstr"
    42. ;;
    43. "3")
    44. echo -n "Enter any string :"
    45. read string
    46. length=$(echo $string | wc -c)
    47. length=$(expr $length - 1)
    48. echo "Length of string is "$length
    49. ;;
    50. "4")
    51. echo "Enter any string :"
    52. string=$(cat) #ctrl + d to terminate
    53. characters=$(echo $string | wc -c)
    54. lines=$(echo "$string" | wc -l)
    55. echo "No of characters : $characters"
    56. echo "No of lines : $lines"
    57.  
    58. ;;
    59. "5")
    60. echo "Enter any string :"
    61. read string
    62. #echo $(echo $string | rev ) 1-solution
    63. length=$(echo $string | wc -c)
    64. length=$(expr $length - 1)
    65. i=$length
    66. s=""
    67. while [ $i -gt 0 ]
    68. do
    69. s=$s$(echo $string | cut -c$i)
    70. i=$(expr $i - 1)
    71. done
    72. echo "Reverse of the string is $s"
    73. ;;
    74. [eE])
    75. exit;
    76. ;;
    77. *)
    78. echo "Invalid choice"
    79. ;;
    80. esac
    81. read temp
    82. clear
    83. echo "==================== AVAILABLE CHOICES ========================="
    84. echo "1. Compare two strings"
    85. echo "2. Join two strings"o
    86. echo "3. Find length of a given string"
    87. echo "4. Occurance of character and word in a string"
    88. echo "5. Reverse the string"
    89. echo "E. Exit"
    90. echo "================================================================"
    91. echo "Enter your choice :"
    92. read choice
    93. done