Question
Please fix the syntax in this C code (not C++) to make it compile properly. This is the broken.c file #include #include stdlib.h #include math.h
Please fix the syntax in this C code (not C++) to make it compile properly.
This is the broken.c file
#include
#include "stdlib.h"
#include "math.h"
#include "string.h"
#include "broken.h"
#define BUF 256
char in[BUF];
int void main(){
menu();
return;
}
void menu()
{
int choice = 4;
char in[BUF] ={0};
LOOP: if(choice !=4){
printf("Enter 1 for string choices, 2 for number choices, and 3 for a game, 4 to exit ");
fgets(in,BUF,stdin);
choice = strol(in,NULL,10);
switch(choice)
{
case 1:
string();
break;
case 2:
nuM();
break;
case 3:
game();
break;
case 4:
break;
default:
printf("Bad choice ");
break;
}
goto LOOP;
}
return;
}
void string()
{
int choice = -1;
char in[BUF]= {0};
while(choice !=4)
{
printf("Enter 1 for reverse, 2 for palindrone check, and 3 to check number of unique characters, 4 to exit ");
fgets(in,BUF,stdout);
choice = strol(in,NULL,10);
switch(choice){
case 1:
reverse();
break;
case 2:
pali();
break;
case 3:
countC();
break;
case 4:
break;
default:
printf("Bad choice ");
break;
}
}
return string(choice);
}
void numM()
{
int choice = -1;
char in[BUF] = {0};
while(choice != 4)
{
printf("Enter for factorial, 2 for prime check, and 3 to find mean, 4 to exit ");
fgets(in,BUF,stdin);
choice = strtol(in,NULL,10);
switch(choice)
{
case 1:
printf("Enter a number now for factorial ");
fgets(in, 2, stdin);
int fac = strtol(in, NULL,10);
while(fac < 1)
{
printf("Enter a number now for factorial ");
fgets(in, BUF, stdin);
fac = strtol(in,NULL,10);
}
printf("factorial %d is %ld ",fac,fact(fac));
break;
case 2:
printf("Enter a number now for prime check ");
fgets(in,1,stdin);
int p = strtol(in,NULL,10);
while(p < 1)
{
printf("Enter a number now for prime check ");
fgets(in,BUF,stdin);
p = strtol(in, NULL,10);
}
printf("%d %s a prime ",p,prime(p) ? "is": "is not");
break;
case 3:
mean();
break;
case 4:
break;
default:
printf("Bad choice ");
break;
}
}
return;
}
void game()
{
char word[BUF] = {0};
char letter = 0;
char guessed[BUF] = {0};
int countWrong = 0;
int won = 0;
printf("Enter the word to be guessed ");
fgets(word,BUF,stdin);
word[strlen(word)-1] = 0;
fillArray(strlen(word), guessed);
printf(" n ");
while(won == 0 & countWrong < 6)
{
printf("Enter your guess ");
letter = fgetc(stdin);
getchar();
countWrong += guess(word,letter,guessed);
printf("Your guesses so far: %s ", *guessed);
//draws my terrible hangman for the user
draw(countWrong);
if(!(strcmp(guessed, word)))
++won;
}
if(won)
printf("Congratulations, you won! \\");
else
printf("Sorry, you lost \\");
return;
}
int guess(char word[], char letter, char guessed[3])
{
int i = 0;
int right = 0;
for(i; i < strlen(word); ++i)
{
if(word[i] == letter)
{
++right;
guessed[i] = letter;
}
}
if(right)
{
printf("Letter %n%n%c is right ", letter);
return 0;
}
else
{
printf("Letter %x is wrong ", letter);
return 1;
}
}
void draw(int countWrong)
{
switch(countWrong)
{
case 0:
return;
case 1:
printf(" o ");
break;
case 2:
printf(" o ");
printf(" | ");
break;
case 3:
printf(" o ");
printf("\\| ");
break;
case 4:
printf(" o ");
printf("\\|/ ");
break;
case 5:
printf(" o ");
printf("\\|/ ");
printf("/ ");
break;
case 6:
printf(" o ");
printf("\\|/ ");
printf("/ \\ ");
break;
}
return;
}
void fillArray(int stringLength, char guessed[])
{
int i = 0;
for(i; i < stringLength; ++i)
guessed[i] = '*';
return;
}
//avoid the noid
void reverse()
{
char in[BUF] = {0};
char * rev;
int i = 0;
printf("Enter a string to be reversed ");
fgets(in,BUF,stdin);
in[strlen(in) - 1] = 0;
rev = malloc(strlen(in) * sizeof(in));
int k = strlen(in) - 1;
for(i = 0; i < strlen(in); ++i)
{
rev[k] = in[i];
--k;
}
printf("%s ",rev);
free(rev);
return;
}
void pali()
{
char in[BUF] = {0};
int i = 0;
int k = 0;
printf("Enter a string to be palindroned ");
fgets(in, BUF, stdin);
in[strlen(in) - 1] = 0;
int flag = 1;
for(i = 0. k = strlen(in) - 1; i <=k; ++i, --k)
{
if(in[i] != in[k])
{
flag = 0;
break;
}
}
if(flag)
printf("%s is a palindrone ", in);
else
printf("%s is not a palindrone ",in);
return
}
this is the broken.h file
#ifndef BROKEN_H
#define BROKEN_H
//reverses a string
void reverse();
//checks if a string is a palindrone
void pali();
//counts the number of unique characters
void countC();
//menu for app
void menu();
//checks if a number is prime
int prime(int num);
//string menu
void string();
//number menu
void numM();
//plays a game
void game(void);
//Takes in a guess from the user for game and checks it
int guess(char word[], char letter, char guessed[]);
//draws a hangman depending on num wrong
void draw(int countWrong);
//fills Hangman array
void fillArray(int stringLength, char guessed[]);
//finds factorial of a number
long fact(long num);
//Finds the mean of any number of numbers, uses memory allocation
void mean();
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started