Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Data Structures Lab1 2020 Ce 118 PDF

The document describes a program that allows a user to guess a secret word by entering letters one at a time. After each guess, the program updates a guess template to show which letters match those in the secret word. It continues until the user guesses the full word or reaches 7 incorrect guesses. Then it outputs the number of guesses taken.

Uploaded by

Ahmad Baseer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Data Structures Lab1 2020 Ce 118 PDF

The document describes a program that allows a user to guess a secret word by entering letters one at a time. After each guess, the program updates a guess template to show which letters match those in the secret word. It continues until the user guesses the full word or reaches 7 incorrect guesses. Then it outputs the number of guesses taken.

Uploaded by

Ahmad Baseer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

M.

UMER AHSAN 2020—CE-118

LAB: 1
TASK: 1
Write a program that take string as an input attempting to guess the secret word enters
letters one at a time. After each guess, the guess template is updated (if necessary) to show
which letters in the secret word match the letter guessed. This process continues until the
guess template matches the secret word choice of 7 wrong attempts. The number of
guesses is then output.
CODE:

m
er as
package guess;

co
eH w
import java.util.Scanner;

o.
public class guess {
rs e
ou urc
public static void main(String[] args) {
int tries = 0;
o
aC s

boolean iterated = false;


vi y re

String temp = "";


String holder = "";
ed d
ar stu

Scanner keyboard = new Scanner(System.in);


System.out.println("Enter your secret word:");
is

String word = keyboard.nextLine();


Th

do {
System.out.println("Guess a letter");
sh

String guess = keyboard.nextLine();


for(int i = 0; i < word.length(); i ++) {
if (guess.equals(Character.toString(word.charAt(i)))) {

Data Structures & Algorithm


This study source was downloaded by 100000826580608 from CourseHero.com on 10-17-2021 06:12:54 GMT -05:00

https://www.coursehero.com/file/85064299/DATA-STRUCTURES-LAB1-2020-Ce-118pdf/
M.UMER AHSAN 2020—CE-118

if(!iterated)
temp += Character.toString(word.charAt(i));
else {
holder = Character.toString(temp.charAt(i)).replace("-", guess);
temp = temp.substring(0, i) + holder + temp.substring( i + 1, temp.length());
}
else{
if(!iterated) {
temp += "-";

m
er as
}}

co
eH w
}

o.
tries++; rs e
ou urc
iterated = true;
System.out.println(temp);
o
aC s

if(temp.equals(word)) {
vi y re

System.out.println("You guessed correctly!");


break;
ed d
ar stu

}
}
is

while (tries < 7);


Th

{System.out.println("Game Over");}
}}
sh

Data Structures & Algorithm


This study source was downloaded by 100000826580608 from CourseHero.com on 10-17-2021 06:12:54 GMT -05:00

https://www.coursehero.com/file/85064299/DATA-STRUCTURES-LAB1-2020-Ce-118pdf/
Powered by TCPDF (www.tcpdf.org)

You might also like