Question

In: Computer Science

A sample human input file that contains test values that a human would enter on the...

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;
}

Solutions

Expert Solution

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.


Related Solutions

[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Computing A Raise File Salary.java contains most of a program that takes as input an employee’s...
Computing A Raise File Salary.java contains most of a program that takes as input an employee’s salary and a rating of the employee’s performance and computes the raise for the employee. This is similar to question #3 in the pre-lab, except that the performance rating here is being entered as a String—the three possible ratings are “Excellent”, “Good”, and “Poor”. As in the pre-lab, an employee who is rated excellent will receive a 6% raise, one rated good will receive...
Write a program which reads an input file. It should assume that all values in the...
Write a program which reads an input file. It should assume that all values in the input file are integers written in decimal. Your program should read all integers from the file and print their sum, maximum value, minimum value, and average. Use the FileClient class here (from a previous reading) as an example. You'll need to create a file to be used as input to test your program, as well. Your program should work whether the integers are separated...
Post a Python program that contains an array variable whose values are input by the user....
Post a Python program that contains an array variable whose values are input by the user. It should the perform some modification to each element of array using a loop and then the modified array should be displayed. Include comments in your program that describe what the program does. Also post a screen shot of executing your program on at least one test case.
The input file Each line of the input file will contain a sentence with words separated...
The input file Each line of the input file will contain a sentence with words separated by one space. Read a line from the listed below  and use a StringTokenizer to extract the words from the line. The input file . Mary had a little lamb whose fl33ce was white as sn0w And everywhere that @Mary went the 1amb was sure to go. Read the above that contains a paragraph of words. Put all the words in an array, put the...
The data file contains the Body Mass Index (BMI) for a sample of men and a...
The data file contains the Body Mass Index (BMI) for a sample of men and a sample of women. Two of the columns, OW_male and OW_female code the BMI values as: 0 - if BMI ≤ 25.4 (these are considered “not overweight”); 1 - if BMI >= 25.5 (these are considered “overweight”). (a) Test whether there is sufficient evidence to show that the proportion of overweight males (proportion of males who are overweight) is different than the proportion of overweight...
Write a function redact() takes as input a file name. See secret.txt for the initial test....
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions. FOR PYTHON
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores....
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores. Each line has the following format: LastName FirstName score Load the input file into an array of structs, knowing there is a maximum of 50 participants. Output to output.txt the participants with the top 5 scores, in decreasing order of scores. Each output line should have the following format: FirstName,LastName,score Notes: The name of the input file is acquired through standard input. If the...
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores....
C++ Programming Consider an input file that lists test participants in alphabetical order and their scores. Each line has the following format: LastName FirstName score Load the input file into an array of structs, knowing there is a maximum of 50 participants. Output to output.txt the participants with the top 5 scores, in decreasing order of scores. Each output line should have the following format: FirstName,LastName,score Notes: The name of the input file is acquired through standard input. If the...
The data file contains displacement (in mm)-load (in N) data for a mechanical test that was...
The data file contains displacement (in mm)-load (in N) data for a mechanical test that was conducted on an unknown metal. The initial length and diameter of the specimen are also given. a. (5 pts.) Using the data and a computer program (such as Excel), create an engineering stress-engineering strain graph with proper labels. The stress axis should be in the units of MPa. You do not need to show your spreadsheet or software code used to make the graph....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT