Amrendra Kumar
Amrendra Kumar

Reputation: 81

I need to print 1st and every 5th character from a given String in java

I have a String of a length of more than 1000 character. In this string i have to print 1st character after that every 5th character.

I tried writing a program to iterate from 0th character to last character and have a count variable.

If count is equal to 5. I am printing the character and count is initializing with 0.

private static String getMaskedToken(String token) {
    if (token == null)
        return null;
    char[] charArray = token.toCharArray();
    int length = token.length();
    StringBuilder sb = new StringBuilder();
    int count = 0;
    for (int i = 0; i < length; i++) {
        count++;
        if (i == 0 || i == length - 1) {
            sb.append(charArray[i]);
        } else if (count == 5) {
            sb.append(charArray[i]);
            count=0;
        } else if(count < 5 && i == length-1){
            sb.append(charArray[i]);
        }else {
            sb.append('*');
        }
    }
    return sb.toString();
}

Need to print last character if count is less than 5 of last iteration.

If String of length 9, "12345678" then actual output will be like 1***5**8

If String of length 9, "123456789abcd" then actual output will be like 1***5****a**d

Upvotes: 1

Views: 667

Answers (4)

Teik Jun
Teik Jun

Reputation: 391

Your code should work fine. Here's an alternative without using StringBuilder and with fewer checks.

private static String getFirstFifthLast(String str) {
    String[] strArray = str.split(""); //returns an array of strings with length 1
    int arrayLength = strArray.length;
    String result = strArray[0]; //append the first element
    //append element if it is in 5th position, append "*" otherwise
    for (int i = 0; i < arrayLength; i++) {
        if ((i + 1) % 5 == 0) {
            result += strArray[i];
        } else {
            result += "*";
        }
    }
    result += strArray[arrayLength - 1]; //append the last element
    return result; 
}

Upvotes: 0

JensW
JensW

Reputation: 442

    String output = "";
    for (int i = 0; i < str.length(); i++) {
        if (i == 0) {
            output += str.charAt(i);
            output += "***";
            output += str.charAt(4);
            i = 4;
        } else if ((i - 4) % 5 == 0) {
            output += str.charAt(i);
        } else if (i == str.length()-1) { 
            output += str.charAt(i);
        } else {
            output += "*";
        }
    }
    System.out.println(output);
}

This will print 1***5****a**d for string "123456789abcd".

Upvotes: 2

Tenusha Guruge
Tenusha Guruge

Reputation: 2310

Try this code,

private void printEvery5thCharacter(String str) {
    for (int i = 1; i < str.length() - 1; i += 5) {
        System.out.print(str.charAt(i - 1) + "***");
        if (i == 1) {
            i = 0;
        }
    }
    if (str.length() % 5 > 0) {
        System.out.print(str.charAt(str.length() - 1));
    }
 }

Upvotes: -1

Java Learner
Java Learner

Reputation: 41

try this code:

public void printFirstAndEveryFifthCharecter(String str) 
{
    for (int i = 0 ; i < str.length() ; i++) 
    {
       if ((i+1) == 1 | (i+1) % 5 == 0) {
          System.out.print(str.charAt(i) + "***");
       }
    }
    if (str.length() % 5 > 0) {
       System.out.print(str.charAt(str.length() - 1));
    }
 }

Upvotes: 0

Related Questions