You are on page 1of 7

Name: Necatican Toklaç

StudentID: 2019502075
Date:10.03.2022
Description: EED1010 Laboratory Work #2

Task 1:
#include <stdio.h>
#include <string.h>
#define SIZE 100
int main()
{
//defining arrays
char s1[SIZE];
char s2[SIZE];

puts("Please enter two words:");


printf("First word:");
fgets(s1,SIZE,stdin);
printf("Second Word:");
fgets(s2,SIZE,stdin);

if(strlen(s1)== strlen(s2)) //Checks words that their lentghs are equal each other
{
if(strspn(s1,s2)== strlen(s1)) //Checks words that their letters are same or not
puts("These two words are anagrams\n"); //displaying result
}
else {
puts("These words are not anagrams");//displaying result
}
}
Output of the programme

Task 2:
#include <stdio.h>

int reverseSentence();//function declaration


int main()
{
printf("Enter a sentence:"); // Getting sentence from user
reverseSentence();
return 0;
}

int reverseSentence() // defining function


{
char c;
scanf("%c",&c);
if(c!='\n')
{
reverseSentence();
printf("%c",c);
}
}
Output of the programme

Task 3:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 20
//Hangman Game

const char* word_database[20] = {"literature",


"scholar","enormous","influence","publication",
"pioneer","telephone","orchestra","teacher","member","final","phonograph","executed",
"oldest","people","requiring","screwdriver","intelligent","different","striking"}; //words for
game
//function declaration
int line(int n);
int show_letter(int n, const char word[]);
int get_check(const char word[]);

int CONTROL[N] = {0};


int main()
{
srand(time(NULL));
int count,error = 1, sum = 0;
printf("###################\n%s\n%s\n%s\n####################\n","HANGMAN
GAME",
"Please press 1 to start/restart the game!","Press 0 to exit from game!");

printf("Write your choice:");


int choice;
scanf("%d",&choice);
// arranging loops for conditions
while(choice !=0)
{
if(choice ==1)
{
int order = rand()%20;
int len = strlen(word_database[order]);
line(len);

while (error<7)
{
count = get_check(word_database[order]);
if (count>0)
{
show_letter(len,word_database[order]);
sum+= count;
if(len==sum)
{
printf("\n***************\nCongratulations!\n");
break;
}
}
if(count==0)
{
error++;
printf("\n!!!\nThe letter is not found\n");
}
}

}
else if(choice!=0 || choice !=1)
{
printf("\nThe game is not started\n");
}
if(error>=7)
{
printf("\n :( You lost!");
}

int i=0;
for(i = 0; i<N;i++)
{
CONTROL[i]=0;
}
count = 0;
error = 1;
sum = 0;
}
printf("!!!\n The game ended!\n");
return 0;
}
int line(int n) //fınction definiton
{
while(n>0)
{
printf("_");
n--;
}
printf("\n");
}
int get_check(const char word[])//function definition
{
char c;
char *p;
int i,n = strlen(word);
int k = 0;

printf("\n>>>>><<<<<\n Guess the letter:");


scanf("%c",&c);
for(i = 0; i<n; i++)
{
if(word[i]==c)
{
k++;
CONTROL[i] = 1;
}
}
return k;
}
int show_letter(int len, const char word[])
{
int i;
for(i=0; i<len; i++)
{
if(CONTROL[i]==1)
printf("%c",word[i]);
else
printf("_");
}
printf("\n");
}

Output of the programme


I could not find my mistake when I was writing this code. I could not get rid of this problem
in output.

You might also like