Finding if a string is in a string in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjbauer95
    New Member
    • Jan 2009
    • 13

    Finding if a string is in a string in C

    I have a function that I want to be able to use to find out if a string is in a string.

    Code:
    int in(char string[], char finder[]){
    	int i = 0;
    	int j = 0;
    	int r = 0;
    	int flag = 0;
    
    	while (string[i] != '\0') {
    		if (flag==0){ // If a part of finder has not yet been found in string
    			if (string[i]==finder[0]){ // check if it is the first letter of finder is this string
    				flag=1;
    			}
    		} else { // The letter has been found
    			if (finder[j]!='\0'){
    				j++;
    				if (finder[j]==string[i]){
    					r=1;
    				} else {
    					r=0;
    				}
    			}
    		}
    		i++;
    	}
    	return r;
    }
    It compiles fine but then I get a Segmentation fault.
    On GDB it says: Program received signal SIGSEGV, Segmentation fault.
    0x08048539 in in ()
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    HI,
    I think there is s array overwrite in the code.
    In K&R C Answer book there is a similar code, take a lok into it if needed for reference.

    Raghu

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Have a look: Knuth Morris Pratt algorithm - Wikipedia, the free encyclopedia.

      kind regards,

      Jos

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Presumably this is for a class otherwise you would just use strstr.

        However, you might consider doing strncmp for corresponding string[i] and
        finder[i].

        Comment

        • mjbauer95
          New Member
          • Jan 2009
          • 13

          #5
          OK.
          I didn't realize that strstr was a function, I can just use that.

          Comment

          • mjbauer95
            New Member
            • Jan 2009
            • 13

            #6
            Still having problems

            Now I am using strstr.
            But I still get a Segmentation Fault.

            My code is this:
            Code:
            #include <stdio.h>
            #include <string.h>
            
            int main(int argc, char **argv) {
            	int i;
            	char string[] = "123456789";
            	char variables[][1024] = {"7"};
            
            	for(i = (sizeof(variables) / 4) - 1; i > -1; --i) {
            		if (strstr(string,variables[i])){
            			printf("%c",variables[i]);
            		}
            	}
            
            	return 0;
            }

            Comment

            • looker
              New Member
              • Dec 2007
              • 18

              #7
              I don't understand why you try to make thing complicated.
              check it here strstr - C++ Reference
              However, if you're required to find the occurrence of a string in another one, you should better consider Knuth Morris Pratt algorithm​​​​​​ ​​​​​​​​​​ ​​​​​ what he said above.
              In your previous code, I found something wrong with your function. You should never trust these two strings inputted, No one guarantees that those strings are NULL terminated; because it could be possibly that you write string in the memory and another process could override the NULL character with whatever it wants. It is because that function will stop when it reach the NULL character, but what happen if there is no NULL character, or that NULL character was stored at nowhere ?
              If you want to write such kind of function, you had better put the size of strings inputted. This will be more safer and can trust.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                The for loop should just range from i = 0 to i<1024.

                Then the printf() parameter should be %s rather than %c since every element of variables is a string.

                Then you need to check that variables[i] os not null befor you call strstr. Otherwise you crash.

                Comment

                Working...