ChallengeToken concatenation and character replacement:

 String Challenge

Have the function StringChallenge(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567"

Once your function is working, take the final output string and concatenate it with your ChallengeToken, and then replace every fourth character with an underscore.


Your ChallengeToken: 386emsol49

Examples

Input: "fun&!! time"

Output: time

Final Output: tim_386_mso_49

Input: "I love dogs"

Output: love

Final Output: lov_386_mso_49

--------------------------------

Java code to implement the StringChallenge function as described, along with the ChallengeToken concatenation and character replacement:

public class Main {
    public static void main(String[] args) {
        System.out.println(StringChallenge("fun&!! time"));
        System.out.println(StringChallenge("I love dogs"));
    }

    public static String StringChallenge(String sen) {
        // Remove punctuation and split the input string into words
        String[] words = sen.replaceAll("[^a-zA-Z0-9 ]", "").split("\\s+");

        // Initialize variables to keep track of the longest word and its length
        String longestWord = "";
        int maxLength = 0;

        // Iterate through the words and find the longest one
        for (String word : words) {
            if (word.length() > maxLength) {
                longestWord = word;
                maxLength = word.length();
            }
        }

        // Concatenate the ChallengeToken
        String finalOutput = longestWord + "386emsol49";

        // Replace every fourth character with an underscore
        StringBuilder sb = new StringBuilder(finalOutput);
        for (int i = 3; i < sb.length(); i += 4) {
            sb.setCharAt(i, '_');
        }

        return sb.toString();
    }
}                                                                                    

Comments

Popular posts from this blog

create image slider using phyton in web

Tahukah kamu Algoritma Genetika dan Penerapannya dalam Industri

create animated futuristic profile card using html+css+js

CRUD SPRING REACTIVE WEBFLUX +Mongo DB

Top 7 Digital Transformation Companies

100 perusahaan perangkat lunak (software) populer dari Eropa dan Amerika yang memiliki kehadiran atau operasional di Indonesia.

TOP 8 Framework Populer menggunakan bahasa .NET

Python Date and Time Manipulation

TOP 5 Trends Programming 2024

Daftar Kata Kunci (Keyword) dalam Bahasa Pemrograman Python