Jump to content

Search for a string in C

- - - - -

  • Please log in to reply
3 replies to this topic

#1
kashiqirphan

kashiqirphan

    Newbie

  • Members
  • Pip
  • 7 posts
In in the learning phase of C and I have the following problem
I have an two dimentional array of charater that is used to store a list of employee names and I wish to search the list for a particular name... Can this be done by using the following set of codes ???

printf("Enter Employee Name to Search Details:");
scanf("%s",&SEmpNam);
for(i=0;i<no;i++)
{
 for(j=0;j<20;j++)
 {
  // EmpNum is the varible that I have used to store the list of employees
  if(SEmpNam[j]==EmpNum[i][j])
  {
   printf("Result Found \n");
  }
 }
}


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You appear to be comparing character by character (SEmpNam[j]) - you should use strcmp to compare strings (i.e. strcmp(SEmpNam, EmpNum[i]) == 0 for equality.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
kashiqirphan

kashiqirphan

    Newbie

  • Members
  • Pip
  • 7 posts

Alexander said:

You appear to be comparing character by character (SEmpNam[j]) - you should use strcmp to compare strings (i.e. strcmp(SEmpNam, EmpNum[i]) == 0 for equality.)

I tried using your idea, I don`t get any compilation error but when i try to execute the program terminates

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
scanf("%s",&SEmpNam);
is SEmpNam is a char array, then & shouldn't be there in scanf because array are passed by reference.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users