Toggle All Character of String

A toggle is an act of switching on and off or reversing the current state to another via the same button.

package org.wesome.dsalgo;

public class Toggle {
    public static String toggle(String string) {
        char[] appleChar = string.toCharArray();
        StringBuilder toggle = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            if (i % 2 == 0) {
                toggle.append(Character.toLowerCase(appleChar[i]));
            } else {
                toggle.append(Character.toUpperCase(appleChar[i]));
            }
        }
        return new String(toggle);
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ToggleTest {
    @Test
    void toggle1() {
        String string = "wesome.org";
        Assertions.assertEquals("wEsOmE.OrG", Toggle.toggle(string));
    }

    @Test
    void toggle2() {
        String string = "I am Learning Ds Algo From WeSome.Org";
        Assertions.assertEquals("i aM LeArNiNg dS AlGo fRoM WeSoMe.oRg", Toggle.toggle(string));
    }

    @Test
    void toggle3() {
        String string = "i aM LeArNiNg dS AlGo fRoM WeSoMe.oRg";
        Assertions.assertEquals("i aM LeArNiNg dS AlGo fRoM WeSoMe.oRg", Toggle.toggle(string));
    }
}
plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on