12,299,332 members (54,430 online)
Rate this:
See more:
how to do horizontal and vertical search for a word in a 2 dimensional character Array using c
Posted 28-Mar-13 0:50am
ThePhantomUpvoter 28-Mar-13 7:01am

You would write some code in C.... do you have a question or problem with some of the code that you have written?
ayesha hassan 28-Mar-13 7:40am

What have you tried so far? Do you have any piece of code that you tried? Please share that as well.

Rate this:

## Solution 1

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But this isn't difficult - think about it as two problems: a "find it in the rows" and a "find it in the columns". Each needs a loop to check each row/column, and then it's just a "find the string in the one dimensional array" repeated.

Try it by hand, on paper, and see what you have to do. It's not difficult to work out!
Rate this:

## Solution 2

Try this and make changes accordingly!

```char *arr[2][2] ;
arr[0][0]="Apple";
arr[0][1]="Book";
arr[1][0]="Cat";
arr[1][1]="Door";
for(int i=0;i<=1;i++)
{
for(int j=0; j<=1 ; j++)
{
cout<<arr[i][j]<<endl;
}
}
cout<<"\t\t\tSearching for character 'Cat'"<<endl;
for(int i=0;i<=1;i++)
{
for(int j=0; j<=1 ; j++)
{
if(arr[i][j] == "Cat")
{
cout<<"\t\t\t*****Search Complete*****\nCharacter found at location ["<<i<<"]"<<"["<<j<<"]"<<endl;
}
}
}```
nv3 28-Mar-13 8:14am

He didn't say 2-dim array of strings, but 2-dim array of characters. And btw. you string comparison will not work as you are comparing pointers and not strings. It might work accidentally if the compiler folds the two "Cats" strings and puts them in the same place. Otherwise, it's going to fail.

Top Experts
Last 24hrsThis month
 ppolymorphe 345 OriginalGriff 308 CHill60 176 Dave Kreskowiak 105 Karthik Bangalore 95
 OriginalGriff 9,733 ppolymorphe 5,861 CHill60 5,452 Karthik Bangalore 5,056 Richard MacCutchan 4,924