Reverse a String

package org.wesome.dsalgo;

public class StringReverse {
    static String reverse(String string) {
        StringBuilder stringBuilder = new StringBuilder(string);
        return stringBuilder.reverse().toString();
    }
}
package org.wesome.dsalgo;

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

public class StringReverseTest {
    @Test
    void reverseLowerCase() {
        StringReverse stringReverse = new StringReverse();
        String string = stringReverse.reverse("wesome.org");
        Assertions.assertEquals("gro.emosew", string);
    }

    @Test
    void reverseMixedCase() {
        String reverse = StringReverse.reverse("WeSoMe.OrG");
        Assertions.assertEquals("GrO.eMoSeW", reverse);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

package org.wesome.dsalgo;

public class StringReverse {
    static String reverse(String string) {
        char temp;
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length / 2; i++) {
            if (chars[i] != chars[chars.length - i - 1]) {
                temp = chars[i];
                chars[i] = chars[chars.length - i - 1];
                chars[chars.length - i - 1] = temp;
            }
        }
        return String.valueOf(chars);
    }
}
package org.wesome.dsalgo;

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

public class StringReverseTest {
    @Test
    void reverseLowerCase() {
        StringReverse stringReverse = new StringReverse();
        String string = stringReverse.reverse("wesome.org");
        Assertions.assertEquals("gro.emosew", string);
    }

    @Test
    void reverseMixedCase() {
        String reverse = StringReverse.reverse("WeSoMe.OrG");
        Assertions.assertEquals("GrO.eMoSeW", reverse);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}
package org.wesome.dsalgo;

public class StringReverse {
    static String reverse(String string) {
        int start = 0;
        int end = string.length() - 1;
        char[] chars = string.toCharArray();
        // Iterate loop upto start not equal to end
        while (start < end) {
            // XOR for swapping the variable
            chars[start] ^= chars[end];
            chars[end] ^= chars[start];
            chars[start] ^= chars[end];
            ++start;
            --end;
        }
        return String.valueOf(chars);
    }
}
package org.wesome.dsalgo;

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

public class StringReverseTest {
    @Test
    void reverseLowerCase() {
        StringReverse stringReverse = new StringReverse();
        String string = stringReverse.reverse("wesome.org");
        Assertions.assertEquals("gro.emosew", string);
    }

    @Test
    void reverseMixedCase() {
        String reverse = StringReverse.reverse("WeSoMe.OrG");
        Assertions.assertEquals("GrO.eMoSeW", reverse);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}
package org.wesome.dsalgo;

public class StringReverse {
    static String reverse(String string) {
        char[] chars = string.toCharArray();
        String reverse = "";
        for (int i = chars.length - 1; i >= 0; i--) {
            reverse += chars[i];
        }
        return reverse;
    }
}
package org.wesome.dsalgo;

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

public class StringReverseTest {
    @Test
    void reverseLowerCase() {
        StringReverse stringReverse = new StringReverse();
        String string = stringReverse.reverse("wesome.org");
        Assertions.assertEquals("gro.emosew", string);
    }

    @Test
    void reverseMixedCase() {
        String reverse = StringReverse.reverse("WeSoMe.OrG");
        Assertions.assertEquals("GrO.eMoSeW", reverse);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

package org.wesome.dsalgo;

public class StringReverse {
    static String reverse(String string) {
        char[] stringChar = string.toCharArray();
        char[] stringRevChar = string.toCharArray();
        for (int i = stringChar.length - 1, j = 0; i >= 0 && j <= stringChar.length; j++, i--) {
            stringRevChar[j] = stringChar[i];
        }
        return String.valueOf(stringRevChar);
    }
}
package org.wesome.dsalgo;

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

public class StringReverseTest {
    @Test
    void reverseLowerCase() {
        StringReverse stringReverse = new StringReverse();
        String string = stringReverse.reverse("wesome.org");
        Assertions.assertEquals("gro.emosew", string);
    }

    @Test
    void reverseMixedCase() {
        String reverse = StringReverse.reverse("WeSoMe.OrG");
        Assertions.assertEquals("GrO.eMoSeW", reverse);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}
body_4
package org.wesome.dsalgo;

public class StringReverse {
    static String reverse(String string) {
        String reverse = "";
        if (string.length() == 1) {
            return string;
        } else {
            reverse = reverse + string.charAt(string.length() - 1) + reverse(string.substring(0, string.length() - 1));
        }
        return reverse;
    }
}
package org.wesome.dsalgo;

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

public class StringReverseTest {
    @Test
    void reverseLowerCase() {
        StringReverse stringReverse = new StringReverse();
        String string = stringReverse.reverse("wesome.org");
        Assertions.assertEquals("gro.emosew", string);
    }

    @Test
    void reverseMixedCase() {
        String reverse = StringReverse.reverse("WeSoMe.OrG");
        Assertions.assertEquals("GrO.eMoSeW", reverse);
    }
}
plugins {
    id 'java'
}

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

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

follow us on