Thread: How to check if a string contains a smaller string?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    How to check if a string contains a smaller string?

    I was wondering if there is a function or an easy way of checking to see if a smaller string is inside a bigger string.
    For example,
    Code:
    char *string1 = "hello";
    char *string2 = "asdjasd hello asdjasdj";
    
    if(string1 is in string2) {
        do stuff;
    }
    Is there a function that can replace "string1 is in string2", or an easy way of doing that using for loops?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can use the strstr() function from string.h

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Thanks for that.

    Another question, when i use the fgets() function to read a line from a file or stdin, is there a way i can put everything after the first string into a string of its own.
    For example,
    Code:
    fgets(getLine,20,file); //where the line is "Test apple banana cherry"
    sscanf(getLine,"%s",str);
    sscanf gets the first string "Test", and I could use sscanf fine if i knew how many words would be after "Test". But what if I do not know how many words are after "Test", how would i be able to either
    a) Find out how many strings after Test
    or
    b) Group the 3 strings after "Test" into a string itself (including the whitespace between them)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sscanf(getLine,"%s",str);
    If you have
    char *remainderString;
    int pos;
    sscanf(getLine,"%s%n",str,&pos);


    Then
    remainderString = &str[pos];
    would be a pointer to the rest of the string, after the first word.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    I remember implementing a pretty fast algorithm for something like this,

    Knuth–Morris–Pratt algorithm - Wikipedia, the free encyclopedia

    The key to the algorithm is to remove all redundant checks .. the naive algorithm is O(N*M) but KMP is O(N + M). (where n is len of str1, and m is len of str2)
    Last edited by rodrigorules; 05-15-2010 at 11:00 PM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Would str contain the whole line as a string (ie including the whitespaces), is that what the %n does?
    If not, wouldn't
    remainderString = &getLine[pos];
    be what i want?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    %n means go read the manual page for sscanf rather than posting your guess on a forum (and being corrected).

    No.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    If str does not contain the whole line as a string, how will &str[pos] contain the rest of the string, as str only holds the first word as a string?
    This prints out a blank line when i enter "test tested testing" into stdin.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    	char getLine[50];
    	char str[50];
    	fgets(getLine,50,stdin);
    	char *remainderString;
    	int pos;
    	sscanf(getLine,"%s%n",str,&pos);
            remainderString = &str[pos];
    	printf("%s\n",remainderString);
    	return 0;
    }
    and when I change that with

    Code:
    remainderString = &getline[pos];
    It gives me the remainder string.....
    I was just confused with what %n did, I did look it up before i posted.
    Last edited by Blasz; 05-15-2010 at 11:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM