Logo 
Search:

Dos Articles

Submit Article
Home » Articles » Dos » ScriptsRSS Feeds

String operations like compare strings, concatenate strings, find length, reverse string, Find string and word in a file using while loop

Posted By: Bogohardt Fischer     Category: Dos     Views: 8994


Menu driven script to perform below operations using while loop.
Compare strings
Concatenate strings
Length of a string
Count occurrence of string and word from file
Reverse string

Code for String operations like compare strings, concatenate strings, find length, reverse string, Find string and word in a file using while loop in Dos

whiletruedo
echo *************************************************
echo 1) COMPARE TWO STRINGS
echo 2) Compare strings
echo 3) Concatenate strings
echo 4) Length of a string
echo 5) Count occurrence of string and word from file
echo 6) Reverse string
echo *************************************************
echo Enter Choice:
read ch
echo *************************************************

case $ch in

1)
        echo Enter String1:
        read str1
        echo Enter String2:
        read str2

        if [ $str1 = $str2 ]
        then
                echo String is equal
        else
                echo String is not equal
        fi
        ;;
2)
        echo Enter String1:
        read str1
        echo Enter String2:
        read str2

        str3=$str1$str2
        echo Join String: $str3
        ;;
3)
        length=0
        echo Enter String1:
        read str1
        length=`expr $str1 | wc -c`
        length=`expr $length - 1`
        echo Length: $length
        ;;
4)
        echo Enter String:
        read str

        echo Enter Word to find
        read word

        echo $str | cat > str1.txt

        grep -o $word str1.txt | cat > str2.txt
        count=`grep -c $word str2.txt`

    echo Count: $count
    ;;
5)
        echo Enter String1:
        read str

        len=`expr $str | wc -c`
        len=`expr $len - 1`
        while [ $len -gt 0 ]
        do
                rev=`expr $str | cut -c $len`
                ans=$ans$rev
                len=`expr $len - 1`
        done
        echo Reverse String: $ans
        ;;

*)
        echo Wrong Choice
        ;;
esac    
    echo Do u want to continue?
    read ans
    if [ $ans = n ]
    then
        exit
    fi
done
  
Share: 



Bogohardt Fischer
Bogohardt Fischer author of String operations like compare strings, concatenate strings, find length, reverse string, Find string and word in a file using while loop is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!