Question

In: Computer Science

Checkpoint 8.1 Assume the variable name references a string. Write a for loop that prints each...

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']

Solutions

Expert Solution

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 !!


Related Solutions

Write a Python loop that goes through the list and prints each string where the string...
Write a Python loop that goes through the list and prints each string where the string length is three or more and the first and last characters of the strings are the same. Test your code on the following three versions of the list examples: examples = ['abab', 'xyz', 'aa', 'x', 'bcb'] examples = ['', 'x', 'xy', 'xyx', 'xx'] examples = ['aaa', 'be', 'abc', 'hello'].
Python Assume s is a string of numbers. Write a program that prints the longest substring...
Python Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print: Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
Python 1.)Assume that name is a variable of type String that has been assigned a value....
Python 1.)Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the last character of the value of name. So if the value of name were "Blair" the expression's value would be 'r'. 2.)Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the last three characters of the value of word. So if...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
Write a for loop from 1 to 3 using i as the variable. For each value...
Write a for loop from 1 to 3 using i as the variable. For each value of i: Create a vector x of 10 random numbers between 0 and 1. Create a second vector t which is equal to ten integers from 1 to 10. Plot x versus t in figure 1. Use hold on to keep each plot. Use a different color for the line for each value of i. At the very end, add the text 'time' using...
write a for loop that uses the loop control variable to take on the values 0...
write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10. Execute the program by clicking the Run button at the bottom of the screen. Is the output the same?
Text Wrap Problem Write a program in Python that takes an input string and prints it...
Text Wrap Problem Write a program in Python that takes an input string and prints it as multiple lines of text such that no line of text is greater than 13 characters and words are kept whole. For example, the first line of the Gettysburg address: Four score and seven years ago our fathers brought forth upon this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal Becomes: Four score and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT