Search for a string in C

This is a discussion on Search for a string in C within the C Programming forums, part of the General Programming Boards category; In in the learning phase of C and I have the following problem I have an two dimentional array of ...

  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
    21,931
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,931
    You should show the declaration of SEmpNam. I can tell you that string comparison in C is typically done with strcmp.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,931
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,931
    This:
    Code:
    scanf("%s",&SEmpNam);
    should be:
    Code:
    scanf("%19s", SEmpNam);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,931
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:51 AM
  4. string search
    By wart101 in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2006, 04:19 AM
  5. string search
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2002, 08:03 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21