Parsing a string

A string is a sequential object that has zero, one or many characters. This lab asks you to ask the user for a string and then count the number of digits in the string.

NOTE: You must do this with a WHILE loop, not a FOR.

How can you tell if a character is a digit? If you have a string variable named str, you. can make a Boolean expression that will be True by doing this: str.isdigit() So if I code: str="spam" then str.isdigit() will be False But if I code: str =. "1984" then str.isdigit() will evaluate to True.

The strings will have digits and non-digits mixed. You must look at each character of the string by itself. You do this by indexing the string. If i is an index variable, then str[i] will give you the character at the i-th position in the string (remembering that it is indexed from zero).

Respuesta :

The program is an illustration of loops

Loops are used to perform repetitive and iterating operations.

The program in Java, where comments are used to explain each line is as follows:

import java.util.*;

public class Main{

public static void main(String[] args) {

 //This creates a Scanner object

 Scanner input = new Scanner(System.in);

 //This declares a string, and also gets input for the string

 String word = input.nextLine();

 //This declares all variables

 int count = 0, countChars = 0;

 //This iterates through the characters, using a while loop

 while(countChars <word.length()){

     //This checks if the current character is a digit

     if(Character.isDigit(word.charAt(countChars))){

         //If yes, this increases the count of characters by 1

         count++;

     }

     //This increments the character index

     countChars++;

 }

 //This prints the number of digits

 System.out.print(count);

}

}

At the end of the program, the number of digits are printed.

Read more about similar programs at:

https://brainly.com/question/13762873

ACCESS MORE
EDU ACCESS