Strings

  • Strings

  • JUnit

  • recursion

reverse

package com.company;

import java.util.Scanner;


public class Main {
    public static String reverse(String str){
        String reversed = "";
        for (int i = 0; i < str.length(); i++)
            reversed = str.charAt(i) + reversed;

        return reversed;
    }

    public static void main(String[] args) {
        String str = "hello";
        System.out.println(reverse(str));
    }
}

JUnit

version 2

JUnit on array

Lets add this function

and add some more tests

here we used assertArrayEquals

Exercise - Permutations

version 2

since the version above is 0(n^2) it isn't very efficient, it would be better to use a bucket sort type of approach and that would give of 2*O(n)

Exercise: Remove words from String

Write a function that receives a string and 2 others string to be removed

solution

String... args - means there are an unknow amount of Strings (an array of Strings)

Exercise: Calculate the sum of the numbers appear in a given string

Solution

Exercise: Convert Int to Roman

img
  • I can be placed before V or X, represents subtract one, so IV (5-1) = 4 and 9 is IX (10-1)=9.

  • X can be placed before L or C represents subtract ten, so XL (50-10) = 40 and XC (100-10)=90.

  • C placed before D or M represents subtract hundred, so CD (500-100)=400 and CM (1000-100)=900.

for example

Solution

Source

Last updated

Was this helpful?