Write code to print the location of any alphabetic character in the 2-character string passCode. Each alphabetic character detected should print a separate statement followed by a newline.

Ex: If passCode is "9a", output is:

Alphabetic at 1
import java.util.Scanner;

public class FindAlpha {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
String passCode;

passCode = scnr.next();
/*Your solution goes here */
}
}

Respuesta :

Answer:

for (int i=0; i<passCode.length(); i++) {

           if ((passCode.charAt(i) >= 65 && passCode.charAt(i) <= 90) || (passCode.charAt(i) >= 97 && passCode.charAt(i) <= 122)) {

               System.out.println("Alphabetic at " + i);

           }

}

Explanation:

The rest of the code should be as above.

In order to check if the character is alphabetic or not, we need to use ASCII table. Characters are represented as integers in the ASCII table. For example "a" equals 97, "A" equals 65.

Then all we need to is to check if the character value in ASCII is in alphabetic ranges. If it is, print the index of the character.

ACCESS MORE
EDU ACCESS
Universidad de Mexico