Thread: Searching Strings

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    Searching Strings

    is there a function that searches a string until another string is found in it, and then returns the position of that?

    Example:
    Code:
    ...
    char string1[] "Hello, how are you?";
    char string2[] "are";
    int i = search(string1, string2);
    ..
    If not, then has anybody written a function like that that I can use?
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  2. #2
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Nevermind, I think I've found the correct function: strcspn.
    Sorry for posting before searching again
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    look into
    strstr()
    and
    strchr()
    strstr() - http://www.rt.com/man/strstr.3.html
    strchr() - http://www.rt.com/man/strchr.3.html
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Thx. (strchr looks usefull, too).

    If anyone's wondering, I'm trying to make a virtual machine, and need this function to locate parts of the virtual hard drive file (to locate virtual files in that file).
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As a quick hack, why not create an index file then? Whenever a file is written, store its offsets (start, and end), file name, etc, in a file. Then just sort that or have it in memory, or what not. fseek to the areay, read the block(s) of the file...

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

  6. #6
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Nah: I've already written a function that works fine with what I want to do. But, that's a good idea.

    My only problem now (which I'm asking help for elsewhere) is the bytecode (but, as I said, I'm getting help with that elsewhere). Hopefully, it won't be that hard.
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Actually, I have another question:

    is there a file-writing function that overwrites the data, and leaves the other alone (if the user edit's a file, I'll just want to fseek() to the file, and start editing there, without having to re-write the rest afterwards)
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by kinghajj
    Actually, I have another question:

    is there a file-writing function that overwrites the data, and leaves the other alone (if the user edit's a file, I'll just want to fseek() to the file, and start editing there, without having to re-write the rest afterwards)
    All file I/O will overwrite the current data. What you have to worry about is the relative length of the new data compared to the old data. Files will typically not shrink and expand when you try to update them. The usual solution to this problem is to rewrite the entire file.

  9. #9
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Strcspn doens't work: it doesn't look for what I'm looking for as a whole, but rather looks for the first occurance of any of the characters I give I need a function that finds it as it appears as a whole, not individual characters! I've look, but can't find one
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kinghajj
    Strcspn doens't work: it doesn't look for what I'm looking for as a whole, but rather looks for the first occurance of any of the characters I give I need a function that finds it as it appears as a whole, not individual characters! I've look, but can't find one
    Asked, and answered:
    Quote Originally Posted by chrismiceli
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    I got it to work... thx for all your help...

    now... on to the bytecode!!! (I'll probably be back here soon, lol )
    01011001 01101111 01110101 00100000 01110100 01101111 01101111 1101011 00100000 01110100 01101000 01101001 01110011 00100000 01101101 01110101 01100011 01101000 00100000 01110100 01101000 01101101 01100101 00100000 01110100 01101111 00100000 01110010 01100101 01100001 01100100 00100000 01011001 01101000 01101001 01110011 00111111 00100000 01000100 01100001 01101101 01101110 00100001 00000000

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    char *strchr(const char *s, int c);

    char *strrchr(const char *s, int c);


    DESCRIPTION
    The strchr() function returns a pointer to the first
    occurrence of the character c in the string s.

    The strrchr() function returns a pointer to the last
    occurrence of the character c in the string s.
    Can i ask why does it say "returns a pointer to the last
    occurrence of the character c" when in the example "c" is an integer?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    int main( void )
    {
        int x;
        char y = 'a';
    
        x = y;
        printf("y has the character value of %c, and has the decimal value of %d.\n", y, y );
        printf("x has the character value of %c, and has the decimal value of %d.\n", x, x );
    
        return 0;
    }
    char is an integeral data type. That is to say, it can be represented as an integer.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  3. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  4. Ofstream, Ifstream, Searching files for strings?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2005, 03:45 PM
  5. Searching through strings...
    By C-Duddley in forum C Programming
    Replies: 1
    Last Post: 09-14-2001, 04:26 PM