Friday, July 15, 2022

Peeling concept and Problems on Strings - Java/Kotlin Developer

 Peeling concept and Problems on Strings -  Java/Kotlin Developer

Basics: Brief introduction to strings.

Algorithms: We'll look at various pattern matching algorithms in strings.

Implementation: How to use strings in Kotlin and Java.

Objective: The objective of this topic is to familiarize the learners with Strings.

1. Introduction of Strings

Strings are defined as a stream of characters. Strings are used to represent text and are generally represented by enclosing text within quotes as: "This is a sample string!".

Different programming languages have different ways of declaring and using Strings.

In C/C++, Strings are defined as an array of characters. The difference between a character array and a string is that the string is terminated with a special character ‘\0’.

In java , String is a sequence of characters. objects of String are immutable which means a constant and cannot be changed once created.

Kotlin String Example: 

import java.util.*

fun main() {

    stringExample()

}

fun stringExample() {

    val s = "GeeksforGeeks"

    // or String s= new String ("GeeksforGeeks");

    // Returns the number of characters in the String.

    // or String s= new String ("GeeksforGeeks");

    // Returns the number of characters in the String.

    println("String length = " + s.length)

    // Returns the character at ith index.

    // Returns the character at ith index.

    println(

        "Character at 3rd position = "

                + s[3]

    )

    // Return the substring from the ith index character

    // to end of string

    // Return the substring from the ith index character

    // to end of string

    println("Substring " + s.substring(3))

    // Returns the substring from i to j-1 index.

    // Returns the substring from i to j-1 index.

    println("Substring = " + s.substring(2, 5))

    // Concatenates string2 to the end of string1.

    // Concatenates string2 to the end of string1.

    val s1 = "Geeks"

    val s2 = "forGeeks"

    println("Concatenated string = $s1$s2")

    // Returns the index within the string

    // of the first occurrence of the specified string.

    // Returns the index within the string

    // of the first occurrence of the specified string.

    val s4 = "Learn Share Learn"

    println(

        "Index of Share " +

                s4.indexOf("Share")

    )

    // Returns the index within the string of the

    // first occurrence of the specified string,

    // starting at the specified index.

    // Returns the index within the string of the

    // first occurrence of the specified string,

    // starting at the specified index.

    println(

        "Index of a = " +

                s4.indexOf('a', 3)

    )

    // Checking equality of Strings

    // Checking equality of Strings

    var out = ("Geeks" == "geeks")

    println("Checking Equality $out")

    out = ("Geeks" == "Geeks")

    println("Checking Equality $out")

    out = "Geeks".equals("gEeks ", ignoreCase = true)

    println("Checking Equality $out")

    val out1 = s1.compareTo(s2)

    println("If s1 = s2 $out")

    // Converting cases

    // Converting cases

    val word1 = "GeeKyMe"

    println(

        "Changing to lower Case " +

                word1.lowercase(Locale.getDefault())

    )

    // Converting cases

    // Converting cases

    val word2 = "GeekyME"

    println(

        "Changing to UPPER Case " +

                word1.uppercase(Locale.getDefault())

    )

    // Trimming the word

    val word4 = " Learn Share Learn "

    println("Trim the word " + word4.trim { it <= ' ' })

    // Replacing characters

    val str1 = "feeksforfeeks"

    println("Original String $str1")

    val str2 = "feeksforfeeks".replace('f', 'g')

    println("Replaced f with g -> $str2")

}

Output : 

String length = 13

Character at 3rd position = k

Substring ksforGeeks

Substring = eks

Concatenated string = GeeksforGeeks

Index of Share 6

Index of a = 8

Checking Equality false

Checking Equality true

Checking Equality false

If s1 = s2 false

Changing to lower Case geekyme

Changing to UPPER Case GEEKYME

Trim the word Learn Share Learn

Original String feeksforfeeks

Replaced f with g -> geeksgorgeeks

Java Example:

public class Main {

    public static void main(String[] args) {

        String s= "GeeksforGeeks";

        // or String s= new String ("GeeksforGeeks");

        // Returns the number of characters in the String.

        System.out.println("String length = " + s.length());

        // Returns the character at ith index.

        System.out.println("Character at 3rd position = "

                + s.charAt(3));

        // Return the substring from the ith index character

        // to end of string

        System.out.println("Substring " + s.substring(3));

        // Returns the substring from i to j-1 index.

        System.out.println("Substring = " + s.substring(2,5));

        // Concatenates string2 to the end of string1.

        String s1 = "Geeks";

        String s2 = "forGeeks";

        System.out.println("Concatenated string = " +

                s1.concat(s2));

        // Returns the index within the string

        // of the first occurrence of the specified string.

        String s4 = "Learn Share Learn";

        System.out.println("Index of Share " +

                s4.indexOf("Share"));

        // Returns the index within the string of the

        // first occurrence of the specified string,

        // starting at the specified index.

        System.out.println("Index of a = " +

                s4.indexOf('a',3));

        // Checking equality of Strings

        Boolean out = "Geeks".equals("geeks");

        System.out.println("Checking Equality " + out);

        out = "Geeks".equals("Geeks");

        System.out.println("Checking Equality " + out);

        out = "Geeks".equalsIgnoreCase("gEeks ");

        System.out.println("Checking Equality " + out);

        int out1 = s1.compareTo(s2);

        System.out.println("If s1 = s2 " + out);

        // Converting cases

        String word1 = "GeeKyMe";

        System.out.println("Changing to lower Case " +

                word1.toLowerCase());

        // Converting cases

        String word2 = "GeekyME";

        System.out.println("Changing to UPPER Case " +

                word1.toUpperCase());

        // Trimming the word

        String word4 = " Learn Share Learn ";

        System.out.println("Trim the word " + word4.trim());

        // Replacing characters

        String str1 = "feeksforfeeks";

        System.out.println("Original String " + str1);

        String str2 = "feeksforfeeks".replace('f' ,'g') ;

        System.out.println("Replaced f with g -> " + str2);

    }

}

Output  :

String length = 13

Character at 3rd position = k

Substring ksforGeeks

Substring = eks

Concatenated string = GeeksforGeeks

Index of Share 6

Index of a = 8

Checking Equality false

Checking Equality true

Checking Equality false

If s1 = s2 false

Changing to lower Case geekyme

Changing to UPPER Case GEEKYME

Trim the word Learn Share Learn

Original String feeksforfeeks

Replaced f with g -> geeksgorgeeks

Note : Run my code and you will understand all the strings concept. There is nothing beyond this in String. In Next topic I will cover string algos and other manipulations of strings.


No comments: