Find Uncommon Characters in two Strings

In this Blog, Sharing code to find characters which are not common in the two strings. For example, String1=”iamcoder” and String2=”iamloser” has below uncommon characters .c, d, l, s str1=”iamcoder” # String 1str2=”iamloser” # String 2CharInStr1NtPrInStr2=” #Blank String which will have Characters in String1 which are not present in String2CharInStr2NtPrInStr1=” #Blank String which will have Characters in String2 which are not present in String1m=${#str2} ##Length of a String2n=${#str1} ##Length of a String1i=0j=0while [[ $i -lt $n ]] #loop through all characters of String1doChar=${str1:$i:1}CharPresentInStr1=echo $str2 | grep -i $Char | wc…

Parenthesis Check in Linux

In this blog, Sharing code to check whether Parenthesis or Brackets are correct in a string or not.Consider a String”[()]” should return “Parenthesis check Passed” while String “{()[(]))”should return “Parenthesis check Failed”. CODE EXPLANATION:1. Created two String OpenBracketStr and CloseBracketStr.2. Keep on Appending based on Character present in the String. If its a opening Bracket, it will be added to OpenBracketStr and as we have received Open Bracket type, I am adding a Expected Closed Bracket by myself in String called CloseBracketStr. In case I get 5 Opening bracket consecutively…

Remove all repeated Characters in a String

In this blog, sharing the code to remove consecutively repeated characters in a String. Consider a String “allabouttechnologies” . Here, l and t are repeated Characters. So, after applying logic, it will be like “alaboutechnologies“. str=”alllabouttechnologies” ##Sample Stringnewstr=” ##new Blank Stringn=${#str} ##length of a Sample StringPrevChar=${str:0:1} ##First Character of a Stringi=1while [[ $i -le $n ]] ##Looping Character by CharacterdoNextChar=${str:$i:1}if [[ $PrevChar == $NextChar ]] ##if two consecutive Character matches, it will remove or ignore the matched one and keep repeated Character oncethenecho “Ignore this Character: “$NextCharelsenewstr=${newstr}${PrevChar} ##merging only non-repeated…

Find first repeated character

In this blog, sharing the code to find the first repeated Character in a string. Consider a String “allabouttechnologies“, here l and t are the repeated Characters but l is the first repeated Character. str=”allabouttechnologies” ## Sample Stringn=${#str} ##Length of a StringPrevChar=${str:0:1} ## Set PrevChar as first character of a Stringi=1while [[ $i -lt $n ]] ## Looping through each character of a stringdoNextChar=${str:$i:1} ## Assigning NextChar equals to current Character in the Loopif [[ $PrevChar == $NextChar ]] ## Comparing consecutive Characters of a string in a loop, if…

All Sub Array having sum=0

In this blog, first we need to understand sub array definition and code to find sum of all Sub-Array in an Array. For that, please refer to this blog. Now, we need to find all Sub-Array whose Sum is equal to zero. Consider an array (1 -1 2 1 -3) and there are three sub-array whose sum is zero mentioned below.( 1 -1)(2 1 -3)(1 -1 2 1 -3) Below is the Shell Script which will give all sub-array whose sum is 0 . arr=(1 -1 2 1 -3) ##…

Sum all Sub Array in Linux

Consider Array (1 2 3) , Below are the Sub-Array for this Array and their sum.(1): Sum of this sub-array is 1(2): Sum of this sub-array is 2(3): Sum of this sub-array is 3(1 2): Sum of this sub-array is 3(2 3): Sum of this sub-array is 5(1 2 3): Sum of this sub-array is 6So, there are 6 Sub Array in this Particular Array.Now, we need to find out sum of all these Array using shell scripting.NOTE : You can remove the outer most while loop and just pass…

TWO ARRAYS ARE EQUAL OR NOT

In this blog, sharing code to check whether two arrays are equal or not . Below is the code explanation in 4 steps. 1. Check the length of each Array, if length is same , then we can compare , else exit.2. If length is same for both the Arrays, then arrange them in either ascending or descending order.3. Once arrays are sorted in an order, then compare element of each Array one by one and if elements are same, increment a dummy counter.4. If the value of counter is…

Check whether length of two array is same or not in Linux

In this blog, sharing code to check whether length of two arrays is same or not. arr=(4 3 2 1) ## First Array of 4 elementsarr_2=(4 3 1 10 1) ## Second Array of 5 elementslen_arr=${#arr[@]}len_arr_2=${#arr_2[@]}if [ $len_arr -eq $len_arr_2 ]thenecho “Length of both Array is same”fiif [ $len_arr -ne $len_arr_2 ]thenecho “Length of both Array is not same”fi

SORT ELEMENTS OF AN ARRAY IN LINUX

In this Blog, sharing code to sort the elements of an array in ascending and descending order as well.ASCENDING ORDER arr=(4 3 2 1) ## Array of 4 Elementslen=${#arr[@]} ## It gives the length of an Array arrk=0lngthminusone=$(( $len – 1))while [ $k -lt $len ] ##Outer Loop which will run len-1 times where len is length of Arraydoi=0 ## This variable set to 0 every time inner loop startswhile [ $i -lt $lngthminusone ] ## Inner loop which will arrange an element of an array in ascending order. Outer…

Append elements of an Array to another Array

In this blog, sharing code of appending elements of one Array to another. arr=(1 2 3 4) ## Array of 4 Elementsarr_2=(5 10 12 1) ## Another Array of 4 Elementsi=0len=${#arr_2[@]} ## It gives the length of an Array arr_2while [ $i -lt $len ]doarr+=(${arr_2[i]}) ## Appending elements of arr_2 into array arr.i=$(( $i + 1))doneecho ${arr[*]}