0

String sorting algorithm in shell script.

I have done it for numbers,but not able to do for string.

EX:- I am kohali. It should be like am i kohali

3
  • echo "i am kohal" | tr ' ' '\n' | sort | tr '\n' ' ' Commented Dec 4, 2017 at 11:23
  • In this way i have also done,but i need algorithm of it. #!/bin/bash clear echo "filename" read filename for i in cat $filename; do echo $i done | sort | tr '\n' ' ' Commented Dec 4, 2017 at 11:27
  • Here are some sorting algorithms: rosettacode.org/wiki/Category:Sorting_Algorithms -- I don't think you'll find any of them actually implemented in shell though. Commented Dec 4, 2017 at 16:12

1 Answer 1

2

Below script will help you

 str='I am kohali'
    for i in `echo $str`; do
        echo $i
    done | sort

Below one will accept input from user

read -p "Enter your string:" str
for i in `echo $str`; do
    echo "$i"
done | sort

Output

am I kohali

Sign up to request clarification or add additional context in comments.

1 Comment

I have written the same code above . Can you write algorithm to take input from user and then perform sorting .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.