In: Computer Science
A sample human input file that contains test values that a human would enter on the keyboard (covering both normal cases and boundary/error cases)
My code is to search the puzzle, read human_input.txt and then search the word
I have a problem with this line : while(fgets(humanchar, 1000, humanfile)),
searchfirst(array,height,width,"happy"); is search well but searchfirst(array,height,width,humanchar); is nothing happen
In addition, The input may contain upper and lower case characters, I try to convert it to lowercase, but i have a problem, so help me with it "you should convert them all to lower case when you read them."
And finally help me to reverse the word in each search
human_input.txt
search_puzzle.txt
happy
error
hope
exit
searchpuzzle.txt
10 7
ABCDEFt
SsGKLaN
OPpRcJW
PLDrJWO
ELKJiIJ
SLeOJnL
happyTg
BEoREEa
JFhSwen
ALSOEId
test.c
#include <stdio.h>
#include "functions.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int fileio()
{ FILE *humanfile;
char humanchar[1000];
FILE *ptr_file;
char buf[1000];
int height,width;
char **array; //create double pointer array
// open human_input file
humanfile = fopen("human_input.txt", "r");
if (!humanfile)
{
printf("File is not available \n");
return 1;
}
char filename[10];
// get a name of puzzle file
fscanf(humanfile,"%s",filename);
//open puzzle file
ptr_file =fopen(filename,"r");
if (!ptr_file){
printf("File is not available
\n");
return 1;
}
fscanf(ptr_file,"%d %d",&height,&width); //
get height and weight
printf("%d ",height);
printf("%d",width);
printf("\n");
//allocate array
array = (char **)malloc(sizeof(char *)*height); //
create the array size
for(int i = 0; i<height;i++){
array[i] = (char *)malloc(sizeof
(char) * width);
}
int col = 0, row;
//get a single char into array
for(row=0; row<height; row++){
fscanf(ptr_file, "%s", buf);
for (int i =0; i <=
width;i++){
if (buf[i] !=
'\0'){
array[row][col] = buf[i];
col++;
}
}
col = 0;
}
// print array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width;
j++){
printf("%c",
array[i][j]);
}
printf("\n");
}
//close file
//////////////////////test/////////////////////////////////////////
//
searchfirst(array,height,width,"happy"); // find the letter
// searchfirst(array,height,width,"EId");
// find the letter
//searchfirst(array,height,width,"tNW");
//searchfirst(array,height,width,"RwI");
//searchfirst(array,height,width,"Eed");
//searchfirst(array,height,width,"PDJ");
//searchfirst(array,height,width,"Ryn");
//searchfirst(array,height,width,"OsC");
//searchfirst(array,height,width,"Eea");
//searchfirst(array,height,width,"LhRynJ");
/////////////////////////////////////////////////////////////////////
//get a single char into array
// human input I have problem with this one
while(fgets(humanchar, 1000, humanfile)) {
// printf("%s\n",
humanchar);
searchfirst(array,height,width,humanchar);
}
free(array);
fclose(humanfile);
fclose(ptr_file);
return 0;
}
void searchfirst(char **array,int height,int width,char
str[]){
// go through array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width;
j++){
if (array[i][j]
== str[0]){ /// find the first letter in string
//printf("\nfound %s\n",str);
findwordhorizontal(array,i,j,str); //find the word horizontal
findwordvertical(array,i,j,height,str); // find the word
vertical
findworddiagonal1(array,i,j,height,width,str);
findworddiagonal2(array,i,j,width,str);
}
}
}
}
void findwordhorizontal(char **array,int m,int n,char str[]){
char *buffer = (char *)
malloc(sizeof(char)*(n));
int j = 0;
while (str[j] != '\0'){
buffer[j] = array[m][n+j]; // add
string to buffer
j++;
}
buffer[j] = '\0';//add \0 to ending of string
buffer
int result = strcmp (buffer,str); // comparing
string
if (result==0) // if 2 string equal
{
printf("Found the word horizontal
%s\n", buffer);
}
free(buffer);
}
void findwordvertical(char **array,int n,int m,int height,char str[]){
char *buffer = (char *) malloc(sizeof(char)*(height
));
int j = 0;
while (n<height){
buffer[j] = array[n][m]; // add
string to buffer
buffer[j+1] = '\0';//add \0 to
ending of string buffer
int result = strcmp (buffer,str);
// comparing string
if (result==0) // if 2 string
equal
{
printf("Found
the word vertical %s\n", buffer);
}
n++;
j++;
}
free(buffer);
}
void findworddiagonal1(char **array,int n,int m,int height,int
width,char str[]){
int count;
for (count = 0; str[count]; count++){
// just count length of str
}
int calculate1 = width - m; // width - current
col
int calculate2 = height -n; // height - current
row
if ( calculate1 >= count && calculate2
>= count){ // if current n m have enough length of str: start
searching
char *buffer = (char *)
malloc(sizeof(char)*(calculate1));
int j = 0;
while (j<count){
buffer[j] =
array[n][m]; // add string to buffer
buffer[j+1] =
'\0';//add \0 to ending of string buffer
//printf("%s vs
%s\n",buffer,str);
int result =
strcmp (buffer,str); // compa4 7ring string
if (result==0)
// if 2 string equal
{
printf("Found the word diagonal 1 %s\n",
buffer);
}
n++;
m++;
j++;
}
}
}
void findworddiagonal2(char **array,int n,int m,int width,char
str[]){
int count;
for (count = 0; str[count]; count++){
// just count length of str
}
int calculate1 = width - m; // width - current
col
int calculate2 = n+1; // height - current row
//printf("%d %d
%d\n",calculate1,calculate2,count);
if ( calculate1 >= count && calculate2
>= count){ // if current n m have enough length of str: start
searching
char *buffer = (char *)
malloc(sizeof(char)*(calculate1));
int j = 0;
while (j<count){
buffer[j] =
array[n][m]; // add string to buffer
buffer[j+1] =
'\0';//add \0 to ending of string buffer
//printf("%s vs
%s\n",buffer,str);
int result =
strcmp (buffer,str); // comparing string
if (result==0)
// if 2 string equal
{
printf("Found the word diagonal 2 %s\n",
buffer);
}
n--;
m++;
j++;
}
}
}
int main()
{
fileio();
return 0;
}
Program:
#include <stdio.h>
//#include "functions.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void searchfirst(char **array,int height,int width,char
str[]);
void findwordhorizontal(char **array,int m,int n,char str[]);
void findwordvertical(char **array,int n,int m,int height,char
str[]);
void findworddiagonal1(char **array,int n,int m,int height,int
width,char str[]);
void findworddiagonal2(char **array,int n,int m,int width,char
str[]);
int fileio()
{ FILE *humanfile;
char humanchar[1000];
FILE *ptr_file;
char buf[1000];
int height,width;
char **array; //create double pointer array
// open human_input file
humanfile = fopen("human_input.txt", "r");
if (!humanfile)
{
printf("File is not available \n");
return 1;
}
char filename[10];
// get a name of puzzle file
fscanf(humanfile,"%s",filename);
//open puzzle file
ptr_file =fopen(filename,"r");
if (!ptr_file){
printf("File is not available \n");
return 1;
}
fscanf(ptr_file,"%d%d",&height,&width); // get height and
weight
printf("height = %d\n",height);
printf("width = %d\n",width);
printf("\n");
//allocate array
array = (char **)malloc(sizeof(char *)*height); // create the array
size
for(int i = 0; i<height;i++){
array[i] = (char *)malloc(sizeof (char) * width);
}
int col = 0, row;
//get a single char into array
for(row=0; row<height; row++){
fscanf(ptr_file, "%s", buf);
for (int i =0; i <= width;i++){
if (buf[i] != '\0'){
if(buf[i]>='A' && buf[i]<='Z')
buf[i] = buf[i] + 32;
array[row][col] = buf[i];
col++;
}
}
col = 0;
}
// print array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++){
printf("%c", array[i][j]);
}
printf("\n");
}
//close file
//////////////////////test/////////////////////////////////////////
/* searchfirst(array,height,width,"happy"); // find the
letter
searchfirst(array,height,width,"EId"); // find the letter
searchfirst(array,height,width,"tNW");
searchfirst(array,height,width,"RwI");
searchfirst(array,height,width,"Eed");
searchfirst(array,height,width,"PDJ");
searchfirst(array,height,width,"Ryn");
searchfirst(array,height,width,"OsC");
searchfirst(array,height,width,"Eea");
searchfirst(array,height,width,"LhRynJ"); */
/////////////////////////////////////////////////////////////////////
//get a single char into array
// human input I have problem with this one
while(fscanf(humanfile, "%s", humanchar)!=EOF){
// printf("%s", humanchar);
searchfirst(array,height,width,humanchar);
}
free(array);
fclose(humanfile);
fclose(ptr_file);
return 0;
}
void searchfirst(char **array,int height,int width, char
str[]){
// go through array
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++){
if (array[i][j] == str[0]){ /// find the first letter in
string
//printf("\nfound %s\n",str);
findwordhorizontal(array,i,j,str); //find the word horizontal
findwordvertical(array,i,j,height,str); // find the word
vertical
findworddiagonal1(array,i,j,height,width,str);
findworddiagonal2(array,i,j,width,str);
}
}
}
}
void findwordhorizontal(char **array,int m,int n,char str[]){
char *buffer = (char *) malloc(sizeof(char)*(n));
int j = 0;
while (str[j] != '\0'){
buffer[j] = array[m][n+j]; // add string to buffer
j++;
}
buffer[j] = '\0';//add \0 to ending of string buffer
int result = strcmp (buffer,str); // comparing string
if (result==0) // if 2 string equal
{
printf("Found the word horizontal %s\n", buffer);
}
free(buffer);
}
void findwordvertical(char **array,int n,int m,int height,char str[]){
char *buffer = (char *) malloc(sizeof(char)*(height ));
int j = 0;
while (n<height){
buffer[j] = array[n][m]; // add string to buffer
buffer[j+1] = '\0';//add \0 to ending of string buffer
int result = strcmp (buffer,str); // comparing string
if (result==0) // if 2 string equal
{
printf("Found the word vertical %s\n", buffer);
}
n++;
j++;
}
free(buffer);
}
void findworddiagonal1(char **array,int n,int m,int height,int
width,char str[]){
int count;
for (count = 0; str[count]; count++){
// just count length of str
}
int calculate1 = width - m; // width - current col
int calculate2 = height -n; // height - current row
if ( calculate1 >= count && calculate2 >= count){
// if current n m have enough length of str: start searching
char *buffer = (char *) malloc(sizeof(char)*(calculate1));
int j = 0;
while (j<count){
buffer[j] = array[n][m]; // add string to buffer
buffer[j+1] = '\0';//add \0 to ending of string buffer
//printf("%s vs %s\n",buffer,str);
int result = strcmp (buffer,str); // compa4 7ring string
if (result==0) // if 2 string equal
{
printf("Found the word diagonal 1 %s\n", buffer);
}
n++;
m++;
j++;
}
}
}
void findworddiagonal2(char **array,int n,int m,int width,char
str[]){
int count;
for (count = 0; str[count]; count++){
// just count length of str
}
int calculate1 = width - m; // width - current col
int calculate2 = n+1; // height - current row
//printf("%d %d %d\n",calculate1,calculate2,count);
if ( calculate1 >= count && calculate2 >= count){ //
if current n m have enough length of str: start searching
char *buffer = (char *) malloc(sizeof(char)*(calculate1));
int j = 0;
while (j<count){
buffer[j] = array[n][m]; // add string to buffer
buffer[j+1] = '\0';//add \0 to ending of string buffer
//printf("%s vs %s\n",buffer,str);
int result = strcmp (buffer,str); // comparing string
if (result==0) // if 2 string equal
{
printf("Found the word diagonal 2 %s\n", buffer);
}
n--;
m++;
j++;
}
}
}
int main()
{
fileio();
return 0;
}
human_input.txt
search_puzzle.txt
happy
error
hope
exit
osc
eid
also
Output:
height = 10
width = 7
abcdeft
ssgklan
opprcjw
pldrjwo
elkjiij
sleojnl
happytg
beoreea
jfhswen
alsoeid
Found the word horizontal happy
Found the word diagonal 2 osc
Found the word horizontal eid
Found the word horizontal also
N.B. I have converted the input to lower case. I have modified human_input.txt and add some words, all of them work fine including the word "happy", but the other given words "error", "hope" and "exit" are not found. I think these words are not in the puzzle. I have also checked the puzzle but not get them. Whether you need help the share with me in the comment section.