My Insight

,

Very basic – Reverse a string

Hello There,

When i was in a college (around 2000), i had learned an algorithm which i did not know the stuff that i learn here. They told me, it’s a very basic code that i need to achieve at first semester. I was dealing with a coding problem that involved reversing a string. 😩.

He just give some texts like below.

Input: Numeral string, X.
Output: Numeral string, Y.

Steps:

1. For i from 1 to LEN(X), let Y[i] = X[LEN(X)+1– i].
2. Return Y[1..LEN(X)].

My lecturer was mentioning “pseudocode”. Pseudocode? 😩 What is that again??

Wtf! I really don’t understand what is all about. Can you imagine that? Those texts above are driving me nuts 🀯.

I have a computer science majoring in Information Technology (IT). What i know is, that major is only kind of Microsoft Word if you typing something and if you do calculation you will be using Microsoft Excel.

I DARED TO ASK TO MY LECTURER AND HERE IS THE CONVERSATION I HAD

πŸ‘¦πŸ» : “Sir, what is the purpose of this? (i’m pointing my finger to a whiteboard that has a weird word)”

πŸ‘¨ : “it’s a pseudocode”.

πŸ‘¦πŸ» : “pseudo..what?”

πŸ‘¨ : “it’s a pseudocode. Pseudocode is a detailed yet readable description of what a computer program or algorithm should do. It is written in a formal yet readable style that uses a natural syntax and formatting so it can be easily understood by programmers and others involved in the development process”.

πŸ‘¦πŸ» : “hm..ok”

Let’s give it a try then.

Note : πŸ‘¦πŸ» : (me) πŸ‘¨: (Lecturer)

LETS DO IT!

Since in my college, PHP and Active Server Pages (ASP – Classic) was the major programming language for the web presentation side. There are also Visual Basic (Classic) as a desktop programming.

I didn’t choose a desktop programming to cater the task neither ASP – Classic. I just wanted to explore Linux OS at that time as a glorified open source.

PHP is the only one that fulfills my needs. Then i should go with PHP then. Let’s go!

<?php
$txt        = "www.sandy.id";
$txtArr     = str_split($txt);
$totalChar  = count($txtArr);
$finalChars = "";
for($i=0;$i<=$totalChar;$i++)
	$finalChars .= $txtArr[$totalChar-$i];
echo "reverse string : [".$txt."] into [".$finalChars."]"; ;
?>

Output :

reverse string : [www.sandy.id] into [di.ydnas.www]

Those line codes made me happy. Because that reverse string is a wonderful output that i’ve ever seen!

USING JAVA

Since my recent work is using Java, as per now i am going to converting to Java. It is something like below.

@Test
public void reverseStringTest() {
        String input = "www.sandy.id";
        char[] inputArr = input.toCharArray();
        int length = inputArr.length;
        StringBuffer reverseString = new StringBuffer();
        for(int i = 0;i < length; i++){
            int indexRev = (length-i)-1;
            char c = inputArr[indexRev];
            reverseString.append(c);
        }
        logger.info("reverse string : [{}] into [{}]",input, reverseString);

}

Both code above successfully reverse a string and printed!

If you want to learn about my other note, you might visit here for code and database section, it is still related on what you wanted to learn in all about technology itself.

That’s all folks! Happy learning as always! πŸ₯°πŸ˜


Leave a Reply

Your email address will not be published. Required fields are marked *