0

I have a character array that array have 10 values .I want the alternate character like 1st,3rd,5th..... from that array .Please any one tell me how to do that

1
  • There is no conceivable way in which this is not a homework question. (And a trivial one at that) You really need to try for yourself in order to actually, you know, learn. Commented Feb 17, 2011 at 18:04

4 Answers 4

1
for(int i = 0 ; i < arr.length ; i+=2){
    System.out.println(arr[i]);//alternate chr
}
Sign up to request clarification or add additional context in comments.

Comments

1

This feels like homework, so I'm not going to give you the solution.

I am going to tell you this:

x % y is the modulo operation. It says "If I divide X by Y, what would the integer remainder be?"

Now, if you were to take this knowledge, and the knowledge that when numbers are EVEN numbers their n % 2 == 0, and ODD numbers n % 2 == 1, can you think of a way, inside a loop, to do this?

With this in mind, edit your question to put the code you've attempted (And, don't just copy one of the other answers and put it as yours.)

Solving these problems for CS majors is like weight-lifting for professional athletes. If you just marked down on the sheet what exercises you did, your coach would probably kick you in the jaw, because you can't perform if you don't exercise. These are your exercise!! :)

Comments

0
 for (int i = 1; i < array.Length; i+=2){
     ....
 }

1 Comment

the first character in an array is array[0] and not array[1]
0

Without any code samples we can only guess what you want... Maybe the solution looks like

String s = "";
for(int i=0; i<array.length; i+=2)
  s+=array[i];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.