String Search

This is a discussion on String Search within the C Programming forums, part of the General Programming Boards category; I'm trying to make a search string function, it will search a string if it's found it will return 1 ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    String Search

    I'm trying to make a search string function, it will search a string if it's found it will return 1 or 0 if it's not found.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*
    	A program for searching a string
    */
    int stringSearch(char **str, char *key, int *position, int size);
    int main()
    {
    	char *str[5] ={"World", "Hello", "arcs", "Sober", "Fun"};
    	char *key;
    	int pos;
    	printf("Input the keyword:\n");
    	scanf("%s", key);
    
    	if(stringSearch(str, key, &pos,5))
    	{
    	printf("Found in element: %d of the array\n", pos);
    	}
    	else
    	{
    	printf("Not Found\n");
    	}
    	return 0;
    }
    
    int stringSearch(char *str[], char *key, int *position, int size)
    {
    	int pos;
    	for (pos=0;pos<size ;pos++)
    	if (strcmp(str[pos],key)==0 )
    	{
    		*position = pos;
    		return 1;
    	}
    	return 0;
    }
    But the problem is the code always return 0, no matter what the input is. Anyone can help me?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *key;
    int pos;
    printf("Input the keyword:\n");
    scanf("%s", key);
    Where exactly are you scaning into? In other words, what memory does key point to?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    As Quzah said, key is simply a pointer to a character that doesn't really point to anywhere. Since it doesn't point to anything - what you are trying to do is read in a string into a character pointer which results in a seg fault usually. You have 2 alternatives, 1 is to point key to a string literal like "Hello":
    Code:
    char* key = "Hello";
    You obviously don't want this since you have no room for user input then, the other alternative is to declare an array of characters or malloc() a size of memory for key.
    Code:
    char key[BUFSIZ];
    
    char* key = malloc(BUFSIZ);
    if (key == NULL)
    {
            fprintf(stderr,"Error allocating memory\n");
            exit (1);
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    thanks for the solution, that really help me alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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