Hi...this is the code that I wrote to find a particular string from a 2-d array of characters. It prints F(for found) and N(for Not found).
When I execute this program, it prints N only.

#include<iostream.h>
#include<conio.h>


char SearchString(char[5][10],char[10]);

void main()
{
	clrscr();
	char str1[5][10],str2[10];
	int i;
	cout<<"\nEnter 5 objects:-";
	for(i=0;i<=4;i++)
		cin>>str1[i];
	cout<<"\nEnter the string to be searched:-";
	cin>>str2;
	cout<<"\n\n\n"<<SearchString(str1,str2);
	getch();
}

char SearchString(char str1[5][10],char str2[10])
{
	int i;
	for(i=0;i<=4;i++)
	{
		if(str1[i]==str2)
			return('F');
	}
	return('N');
}

Recommended Answers

All 6 Replies

I don't understand what you want but check this . Its work

#include<iostream>
//#include<conio>

using namespace std;
char SearchString(char[2][10],char[10]);

int main()
{
//std::clrscr();
char str1[2][10],str2[10];
int i,j;
cout<<"\nEnter 5 objects:-";
for(i=0; i<2;i++)
{
    for(j=0;j<10;++j)
    {
cout<<"\nEnter the string";
cin>>str1[i][j];
    }
}
for(i=0; i<10;++i)
{
cout<<"\nEnter char to be searched:-";
cin>>str2[i];
}

cout<<"\n\n\n"<<SearchString(str1,str2);
//getch();
}

char SearchString(char str1[2][10],char str2[10])
{
int i,j;
for(i=0; i<2;i++)
{
    for(j=0;j<10;++j)
    {

if(str1[i][j]==str2[j])
return('F');

    }
}
return ('N');
}
if(str1[i]==str2)

This line is error prone.

I don't understand what you want but check this . Its work

#include<iostream>
//#include<conio>

using namespace std;
char SearchString(char[2][10],char[10]);

int main()
{
//std::clrscr();
char str1[2][10],str2[10];
int i,j;
cout<<"\nEnter 5 objects:-";
for(i=0; i<2;i++)
{
    for(j=0;j<10;++j)
    {
cout<<"\nEnter the string";
cin>>str1[i][j];
    }
}
for(i=0; i<10;++i)
{
cout<<"\nEnter char to be searched:-";
cin>>str2[i];
}

cout<<"\n\n\n"<<SearchString(str1,str2);
//getch();
}

char SearchString(char str1[2][10],char str2[10])
{
int i,j;
for(i=0; i<2;i++)
{
    for(j=0;j<10;++j)
    {

if(str1[i][j]==str2[j])
return('F');

    }
}
return ('N');
}

Hi...perhaps, I should have made the needs of the program clearer.

User has to enter 5 names.
Suppose, he enters:-
Bat
Ball
Bag
Gloves
Pads

Then, the program asks the user for the string to be searched.
Suppose, he enters Bat.

The program should then print "F" on the screen because it found the string "Bat".

if(str1[i]==str2)

This line is error prone.

Hi, then what can be the correct line to replace this one?

Not allowed to use the <string> header?

Hi...thanks everyone for your reply. I ultimately found the solution.

Yes, Agni, we can use string file in the header.

Instead of this:-

1.
if(str1==str2)

It should have been

if(strcmp(str1,str2))//with the header file <string.h> of course.

*Correction

if(strcmp(str1,str2)==0)//with the header file <string.h> of course.

This shall work. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.