讲解 program程序、辅导 c/c++,Java编程
            
                Lab 2 
The objectives of this lab include the understanding of if statements, loops, input, output and 
strings. 
 
Marks: each question is worth 1 mark for a total of 4 marks. 
 
1. Write a program that prompts the user for a password. The password that must be entered is 
“cmput101”. If the user enters a different password, the program will continue to prompt the user 
for the password. If the user enters the correct the password, the program will print “Logging 
in…” and will then exit. You may implement your program with a while loop and use the not 
operator. 
Sample runs: 
Enter your password: cmput303 
Enter your password: math313 
Enter your password: spring2020 
Enter your password: cmput101 
Logging in... 
 
2. Write a program that prompts the user for a fruit name. If the user enters “apple”, “pear”, or 
“berry”, the program will print “We grow” followed by the fruit name that the user entered. But 
if the user enters a different string, the program will print “We don’t grow this fruit!”. You may 
use an if statement and the or operator. 
4 Sample runs: 
Enter a fruit name: apple 
We grow apple 
 
Enter a fruit name: pear 
We grow pear 
 
Enter a fruit name: berry 
We grow berry 
 
Enter a fruit name: mango 
We don't grow this fruit! 
 
 
3. Write a program that prompts the user for a letter and a string. The program outputs (prints) 
the number of occurrences of the letter in the string. 
 
Sample run: 
Enter a letter: p 
Enter a string: apple 
The letter p occurs 2 times in the string apple 
 
 
  
4. Write a program that inputs individual letters and creates a string of these letters except it will 
exclude the letter “x”. To signal the end of the input, the user will input the letter “z”. The final 
string should not include any “x” and should not include the “z”. The program must print the 
final string. 
 
Sample input and output 
Enter a character: o 
Enter a character: x 
Enter a character: k 
Enter a character: a 
Enter a character: x 
Enter a character: y 
Enter a character: z 
The final string is okay 
 
            
         
        
    
        
        
            
                - QQ:99515681 
- 邮箱:99515681@qq.com 
- 工作时间:8:00-21:00 
- 微信:codinghelp
 
     
    
    
    
 
        
        
        
    
        
            联系我们 - QQ: 99515681 微信:codinghelp
        
 
        
        
            程序辅导网!