We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here is my answer following the guidelines of actually reading input into an array first:
i=0whileread val
do
array[$((i++))]=$valdoneecho"${array[*]}"
We loop through the input line-by-line using the while loop and put the value in the ith position in the array. After all input is read, you can print out the entire phrase using echo "${array[*]}"orecho "${array[@]}" (they are equivolent here).
If you are wondering, the array[$((i++))] is simply increasing the value of i after it is used to refer to the ith position in the array. It is the same as writing:
...
do
array[$i]((i++))
...
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Read in an Array
You are viewing a single comment's thread. Return to all comments →
Here is my answer following the guidelines of actually reading input into an array first:
We loop through the input line-by-line using the
while
loop and put the value in thei
th position in the array. After all input is read, you can print out the entire phrase usingecho "
${array[*]}"or
echo "${array[@]}"
(they are equivolent here).If you are wondering, the
array[$((i++))]
is simply increasing the value ofi
after it is used to refer to thei
th position in the array. It is the same as writing: