Book Image

Hands-On Automation Testing with Java for Beginners

Book Image

Hands-On Automation Testing with Java for Beginners

Overview of this book

Java is one of the most commonly-used software languages by programmers and developers. Are you from a non-technical background and looking to master Java for your automation needs? Then Hands-On Automation Testing with Java for Beginners is for you. This book provides you with efficient techniques to effectively handle Java-related automation projects. You will learn how to handle strings and their functions in Java. As you make your way through the book, you will get to grips with classes and objects, along with their uses. In the concluding chapters, you will learn about the importance of inheritance and exceptions with practical examples. By the end of this book, you will have gained comprehensive knowledge of Java.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Reversing a string's logic


In this section, let's see how we can print a string in reverse. This is one of the questions that was asked in the Yahoo interview. Let's create a reversedemo class for our example.

We have a string called Rahul, and we want the output to be luhaR. There is one more concept that we need to be aware of: a palindrome. If you type in a string, such as madam, and we reverse the string, it would just give madam as the output. Such types of strings are called palindromes. One such instance of a palindrome is shown in the following code:

package demopack;

public class reversedemo {

    public static void main(String[] args) {

        String s = "madam";
        String t= "";
        for(int i=s.length()-1; i>=0; i--)
        {
            t= t+ s.charAt(i);
        }
        System.out.println(t);
    }
}    

We would start by creating a string, called s, and an empty string, called t. We create this empty string to concatenate each element after the for loop to get...