Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing String Manipulation Algorithms in Java

Tech 1

Core Concepts for String Operations

Java Input/Output Basics

  • For reading from standard input: Scanner sc = new Scanner(System.in);
  • For writing to standard output: System.out.println();

String Characteristics in Java

  • Strings are immutable objects in Java.
  • The length of a string can be obtained using the length() method: s.length().

Problem 1: Reversing a Character Array

Implement a function that modifies a character array in-place to reverse its order.

Solution Implementation

public class ReverseCharacterSequence {
    public void invertArray(char[] data) {
        int startIdx = 0;
        int endIdx = data.length - 1;
        while(startIdx < endIdx) {
            char swap = data[startIdx];
            data[startIdx] = data[endIdx];
            data[endIdx] = swap;
            startIdx++;
            endIdx--;
        }
    }
}

Problem 2: Custom String Reversal by Interval

Given a string s and an integer k, reverse the first k characters for every 2k characters. If fewer than k characters remain, reverse all remaining characters.

Solution Implementation

public class IntervalReversal {
    public String modifyString(String inputStr, int interval) {
        char[] charSequence = inputStr.toCharArray();
        for (int blockStart = 0; blockStart < charSequence.length; blockStart += 2 * interval) {
            int low = blockStart;
            int high = Math.min(blockStart + interval - 1, charSequence.length - 1);
            while (low < high) {
                char interim = charSequence[low];
                charSequence[low] = charSequence[high];
                charSequence[high] = interim;
                low++;
                high--;
            }
        }
        return new String(charSequence);
    }
}

Problem 3: Digit Replacement in Strings

Read a string from standard input. Replace every digit (0-9) with the word "number". Return the modified string. This problem emphasizes ACM-style input/output hendling.

Solution Implementation

import java.util.Scanner;

public class DigitSubstitution {
    public static void main(String[] args) {
        Scanner inputReader = new Scanner(System.in);
        String original = inputReader.nextLine();
        char[] originalChars = original.toCharArray();
        
        int digitCount = 0;
        for (char ch : originalChars) {
            if (ch >= '0' && ch <= '9') {
                digitCount++;
            }
        }
        
        int newLength = originalChars.length + digitCount * 5;
        char[] resultArray = new char[newLength];
        
        for(int newIndex = newLength - 1, origIndex = originalChars.length - 1; newIndex >= 0; newIndex--, origIndex--) {
            char currentChar = originalChars[origIndex];
            if (currentChar > '9' || currentChar < '0') {
                resultArray[newIndex] = currentChar;
            } else {
                resultArray[newIndex] = 'r';
                resultArray[newIndex-1] = 'e';
                resultArray[newIndex-2] = 'b';
                resultArray[newIndex-3] = 'm';
                resultArray[newIndex-4] = 'u';
                resultArray[newIndex-5] = 'n';
                newIndex -= 5;
            }
        }
        System.out.println(new String(resultArray));
        inputReader.close();
    }
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.