Thread: Search for a string in C

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Question Search for a string in C

    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 ???

    Code:
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kashiqirphan
    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 ?
    Test your code to find out for yourself. Only if your tests pass should you come back to us with this question. If your tests fail, you then have data to ask for specific help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Sorry I did not mention
    I am getting an error that says
    error C2109: subscript requires array or pointer type

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should show the declaration of SEmpNam. I can tell you that string comparison in C is typically done with strcmp.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    I tried using this
    Code:
    if(strcmp(SEmpNam, EmpNum[i]) == 0)
    instead of
    Code:
    if(SEmpNam[j]==EmpNum[i][j])
    Now i don`t get any compilation error but when I tried to execute it the program terminates
    I am using Visual Studio 2005 compiler

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    This is the simplest program I made

    Code:
    #include<stdio.h>
    #include<string.h>
    main()
    {
    	char SEmpNam[20],EmpNam[100][20]={{"Jack"},{"Mike"}};
    	int i,j;
    	printf("Enter Name to Search:");
    	scanf("%s",&SEmpNam);
    	for(i=0;i<100;i++)
    	{
    		for(j=0;j<20;j++)
    		{
    			if(SEmpNam[j]==EmpNam[i][j])
    			{
    				printf("Result Found \n");
    			}
    		}
    	}
    }
    This code is fine when the EmpNam value is defined in the program but does not work when I have to read it`s value from the user

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    scanf("%s",&SEmpNam);
    should be:
    Code:
    scanf("%19s", SEmpNam);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Exclamation

    Quote Originally Posted by laserlight View Post
    This:
    Code:
    scanf("%s",&SEmpNam);
    should be:
    Code:
    scanf("%19s", SEmpNam);
    Still the same. . . . Still Not working

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but there is nothing else that I can see wrong with it. Actually, I can see something that is wrong with it, and I already told you about it earlier (and there's more!), but according to you:
    Quote Originally Posted by kashiqirphan
    This code is fine when the EmpNam value is defined in the program
    so once we fix the input problem, everything else should be okay
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Thanks for helping me laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 06:51 PM
  2. search a string
    By jafa401 in forum C Programming
    Replies: 2
    Last Post: 08-17-2009, 09:35 PM
  3. search a string...
    By compile in forum C Programming
    Replies: 21
    Last Post: 01-16-2007, 04:51 AM
  4. string search
    By wart101 in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2006, 05:19 AM
  5. string search
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2002, 09:03 AM

Tags for this Thread