In: Computer Science
Checkpoint 8.1
Assume the variable name references a string. Write a for loop
that prints each character letter in the string. The actual print
statement is provided.
# What goes here?
print(letter)
Checkpoint 8.11
Write an if statement using the in operator that determines
whether 'd' is in my_string. A print statement that might be placed
after the if statement is included.
# What goes here?
print('Yes, it is there.')
Checkpoint 8.13
Write the first if statement to complete the code
below. The block should display “Digit” if the string
referenced by the variable ch contains a numeric digit. Otherwise,
it should display “No digit.”
# What goes here?
⋅⋅⋅⋅print('Digit')
else:
⋅⋅⋅⋅print('No digit')
(Note: The ⋅⋅⋅⋅ symbols represent indentation.)
Checkpoint 8.15
The code below asks the user “Do you want to repeat the
program or quit? (R/Q)”. A loop should repeat until the user
has entered an R or Q (either uppercase or
lowercase). Replace the second line with the appropriate while
statement.
again = input('Do you want to repeat ' +
⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅'the program or quit? (R/Q) ')
# What goes here?
⋅⋅⋅⋅again = input('Do you want to repeat the ' +
⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅'program or quit? (R/Q) ')
(Note: The ⋅⋅⋅⋅ symbols represent indentation.)
Checkpoint 8.17
A loop counts the number of uppercase characters that appear in
the string referenced by the variable mystring. Replace the second
statement with the appropriate if statement.
for letter in mystring:
⋅⋅⋅⋅# What goes here?
⋅⋅⋅⋅count += 1
(Note: The ⋅⋅⋅⋅ symbols represent indentation.)
Checkpoint 8.18
Assume the following statement appears in a program:
days = 'Monday Tuesday Wednesday'
Write a statement that splits the string, creating the following
list named my_list:
['Monday', 'Tuesday', 'Wednesday']
Checkpoint 8.19
Assume the following statement appears in a program:
values = 'one$two$three$four'
Write a statement that splits the string, creating the following
list named my_list:
['one', 'two', 'three', 'four']
Hello, The question has not specified any language so I am answering in java. I have added COMMENTS and the SAMPLE OUTPUT for each answer . Please find the answer attached below.
ANSWER FOR CHECKPOINT 8.1 -->
// ANSWER FOR CHECKPOINT 8.1
import java.io.*;
import java.util.*;
public class StringLetters{
public static void main(String []args){
String String1 = new String("ABCDEF" ); //this is the string
System.out.print("The String is : " );
System.out.println(String1); // display the string to user
int a=String1.length(); //finding the length of the string
System.out.print("The letters in the string are:" );
for(int i=0;i<a;i++)
{
System.out.print(String1.charAt(i)+ " "); //print each character/letter of the string
}
}
}
/* ------------- SAMPLE OUTPUT -----------------
The String is : ABCDEF
The letters in the string are:A B C D E F
---------------------------------------------*/
ANSWER FOR CHECKPOINT 8.11 -->
//ANSWER FOR CHECKPOINT 8.11-->
import java.io.*;
import java.util.*;
public class StringLetters{
public static void main(String []args){
String String1 = new String("document" ); //this is the string
System.out.print("The String is : " ); // display the string to the user
System.out.println(String1);
int ret=String1.indexOf('d'); //The string.indexOf returns -1 if d is not present in the string
if (ret== -1) // if d is not present ret will be -1
System.out.print("Its not there"); //if d not present show message
else
System.out.print("Yes, it is there"); // if "d" is present show the message
}
}
/* ------------- SAMPLE OUTPUT -----------------
The String is : document
Yes, it is there
---------------------------------------------*/
ANSWER FOR CHECKPOINT 8.13 -->
//ANSWER FOR CHECKPOINT 8.13-->
import java.io.*;
import java.util.*;
public class StringLetters{
public static void main(String []args){
String String1 = "doc5"; //this is the string
boolean digit = false; // this is boolean variable to store state
char[] chars = String1.toCharArray(); //converting string to char array for operation on characters
for(char c : chars){
if(digit=Character.isDigit(c)){ //checking if any character is digit this will store true in digit if a digit is present in the character array of string
break;
}
}
if(digit==true) // if digit is found print digit
System.out.println("digit");
else
System.out.println("no digit"); // if not found no digit is printed
}
}
/* ------------- SAMPLE OUTPUT -----------------
digit
//because digit "5" is present in the string
---------------------------------------------*/
ANSWER FOR CHECKPOINT 8.15-->
//ANSWER FOR CHECKPOINT 8.15-->
import java.util.*;
public class StringLetters{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
while(1){ // This is the loop as asked in the question
System.out.println("Do you want to repeat the program or quit? (R/Q)"); //displaying the options
char ch; //this is the character to store user input
ch = sc.next().charAt(0); // reading the input
System.out.println("your input is :");
System.out.println(ch); //display the input character to user
switch(ch){
case 'r':main(); //any other function can also be called
break;
case 'q': System.exit(0); //succesfully terminate the program or exit the program
break;
}
}
}
}
/* ------------- SAMPLE OUTPUT -----------------
if r is given as input main is called again
and options are displayed
if q is given then program will terminate.
---------------------------------------------*/
THANK YOU !!