Trying to get a loop to work through alternate arrays

Viewed 84

I'm trying to print out a string by alternating its letter cases. I want YourString to come out as YoUrStRiNg. I've tried three things but I can't get the loop to work the way I need it to. Here's what I have so far:

//one attempt
String s = "yourString";
String x = "";

for (int i = 0; i < s.length(); i += 2) {
    for (int j = 1; j < s.length(); j += 2) {
        x += Character.toUpperCase(s.charAt(i));
        x += Character.toLowerCase(s.charAt(j));
    }
}
System.out.println(x);
//the desired result but not the ideal solution
String[] sArray = {"Your", "String"};
String f = "";
for (String n : sArray) {
    f += n;
}

char[] c = f.toUpperCase().toCharArray();
char[] d = f.toLowerCase().toCharArray();

System.out.print(c[0]);
System.out.print(d[1]);
System.out.print(c[2]);
System.out.print(d[3]);
System.out.print(c[4]);
System.out.print(d[5]);
System.out.print(c[6]);
System.out.print(d[7]);
System.out.print(c[8]);
System.out.print(d[9]);
System.out.println();
//third attempt with loop but the first loop keeps starting from zero
String t = "";
for (int i = 0; i < c.length; i += 2) {
    for (int j = 1; j < d.length; j += 2) {
        t += Character.toUpperCase(c[i]);
        t += Character.toLowerCase(d[j]);
    }
    System.out.print(t);
}

What am I doing wrong?

3 Answers

Actually, there's no need to iterate more than once through the elements of the String. As you need to change the case of the character alternatively, you can just count the position of your iteration, by using the operator %. So, for example, given c as the current String character, the operation would be like this:

System.out.print(i % 2 == 0, (char)Character.toUpperCase(c) : (char)Character.toLowerCase(c));

However, you can actually take advantages from Java Stream and lambda expression, so to realize a very elegant solution for that.
I am going to show you my proposal solution. The only issue is that you cannot actually have a proper cycle variable, as the variable you access inside the lamba expression must be final or effective final, so I used a sort of trick for it.
That is just to give you an idea, you can actually personalize, make it reusable, and improve it as you wish:

public class MyStringTest {
    public static void main(String args[]) {
      String s = "yourString";
      
      initializeCycleVariable();
      s.chars().forEach(c -> 
        {System.out.print( MyStringTest.getAndIncrement() %2 == 0 ? 
                           (char)Character.toUpperCase(c) : 
                           (char)Character.toLowerCase(c));
        });
    }
    
    private static int i = 0;
    
    public initializeCycleVariable() {  i = 0; }
    public static int getAndIncrement() { return i++; }
}

And here is the output:

YoUrStRiNg

You should iterate over the string char by char. You could do upper case for the even indexes, and lower case for the odd ones. Sorry for not providing more detail, but it is clear that this is an assignment.

Try this one out,

String s = "yourString", x = "";
for(int i = 0; i < str.length(); i++){

    if(i % 2 == 0)
        x += Character.toUpperCase(s.charAt(i));
    else
        x += Character.toLowerCase(s.charAt(i));
}
System.out.println(x);
Related