lchristina26
lchristina26

Reputation: 205

C Program - Fix bug in printing char array

I am trying to find a 1d array within a 2d array (find a word withing the given 2d array) and I am getting everything correct, except when it prints the find_ten array, it prints ',0' instead of 'ten' and then prints the wrong location of '-1, -1'. Any help in finding where my issue lies would be great. My program keeps printing this:

set found at: 0,0

sat found at: 0,0

ere found at: 0,1

,0  found at: -1,-1

axe found at: -1,-1

Instead of this, which is what I am trying to get to:

set found at: 0,0

sat found at: 0,0

ere found at: 0,1

ten  found at: 2,0

axe found at: -1,-1

Here is the code for main:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "find_word.h"

int main(){

    char find_set[3] = {'s', 'e', 't'};
    char find_sat[3] = {'s', 'a', 't'};
    char find_ere[3] = {'e', 'r', 'e'};
    char find_ten[3] = {'t', 'e', 'n'};
    char find_axe[3] = {'a', 'x', 'e'};
    char two_d[3][3] = {{'s', 'e', 't'}, {'a', 'r', 'e'}, {'t', 'e', 'n'}};

    word_find(two_d, find_set);
    word_find(two_d, find_sat);
    word_find(two_d, find_ere);
    word_find(two_d, find_ten);
    word_find(two_d, find_axe);

    return (0);
}

And the function:

#include <stdio.h>
#include <stdlib.h>
#include "find_word.h"
#include <string.h>

void word_find(char stringof_3 [3][3], char the_word[3]){

    char *row_col;
    int print_letter = 0;
    int word_count = 0;

    if(stringof_3[0][0] == the_word[0] && stringof_3[0][1] == the_word[1] && stringof_3[0][2] == the_word[2])
            strcpy(row_col, "0,0  ");
    else if(stringof_3[0][0] == the_word[0] && stringof_3[1][0] == the_word[1] && stringof_3[2][0] == the_word[2])
            row_col = "0,0  ";
    else if(stringof_3[1][0] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[1][2] == the_word[2])
            row_col = "1,0  ";
    else if(stringof_3[2][0] == the_word[0] && stringof_3[2][1] == the_word[1] && stringof_3[2][2] == the_word[2])
            row_col = "2,0  ";
    else if(stringof_3[0][1] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[2][1] == the_word[2])
            row_col = "0,1  ";
    else if(stringof_3[0][2] == the_word[0] && stringof_3[1][2] == the_word[1] && stringof_3[2][2] == the_word[2])
            row_col = "0,2 ";
    else
            row_col = "-1,-1";

    for(print_letter = 0; print_letter < 3; print_letter++){
            printf("%c", the_word[print_letter]);
    }
    printf(" found at: %s", row_col);
    printf("\n");
}

And the header:

extern void word_find(char stringof_3[3][3], char the_word[3]);  /*Inputs a 2d char array and finds the given 3 letter  word */

Upvotes: 0

Views: 257

Answers (2)

Karthik
Karthik

Reputation: 979

Replace strcpy(row_col, "0,0 "); with row_col= "0,0 ";

Upvotes: 1

Vijay
Vijay

Reputation: 439

You are not allocating space to row_col buffer. You just declared it that is why it is giving you weird output. So please allocate space to row_col buffer. Secondly please use strcpy instead of directly assigning the value to row_col buffer. Below is the code for word_find function with these two errors corrected.

void word_find(char stringof_3 [3][3], char the_word[3]){

    char *row_col;
    row_col = malloc(6);   // allocate space to buffer
    int print_letter = 0;
    int word_count = 0;

    if(stringof_3[0][0] == the_word[0] && stringof_3[0][1] == the_word[1] && stringof_3[0][2] == the_word[2])
            strcpy(row_col, "0,0  ");
    else if(stringof_3[0][0] == the_word[0] && stringof_3[1][0] == the_word[1] && stringof_3[2][0] == the_word[2])
            strcpy(row_col, "0,0  ");
    else if(stringof_3[1][0] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[1][2] == the_word[2])
            strcpy(row_col, "1,0  ");
    else if(stringof_3[2][0] == the_word[0] && stringof_3[2][1] == the_word[1] && stringof_3[2][2] == the_word[2])
            strcpy(row_col, "2,0  ");
    else if(stringof_3[0][1] == the_word[0] && stringof_3[1][1] == the_word[1] && stringof_3[2][1] == the_word[2])
            strcpy(row_col, "0,1  ");
    else if(stringof_3[0][2] == the_word[0] && stringof_3[1][2] == the_word[1] && stringof_3[2][2] == the_word[2])
            strcpy(row_col, "0,2 ");
    else
            strcpy(row_col, "-1,-1");

    for(print_letter = 0; print_letter < 3; print_letter++){
            printf("%c", the_word[print_letter]);
    }
    printf(" found at: %s", row_col);
    printf("\n");
}

Upvotes: 1

Related Questions