Results 1 to 4 of 4

Thread: A find in string function

  1. #1

    Thread Starter
    Lively Member dubae524's Avatar
    Join Date
    Jun 2001
    Location
    Currently on this Super Star Destroyer being telekinetically strangled to death by Darth Vader
    Posts
    78

    A find in string function

    int findinstring(const char *inner, const char *outer, int start) {
    int x = 0; // When it begins to count within the substring
    bool searchflag = false; // To see if it's been found

    for(int i = start; outer[i] != 0; i++) {
    searchflag = true; // Sets it true, for now
    for(x = 0; inner[x] != 0; x++)
    if(outer[i + x] != inner[x])
    searchflag = false;
    /* ^ Checks to see if every occurence is true.

    If but one is false, it will then set searchflag to false saying to continue the search. */
    if (searchflag == true) // Tells it to quit the loop in this case, because we're now done.
    break;
    }

    if (searchflag == true)
    return i; // Returns pos if it could find one
    else
    return -1; // This is what it returns if it could not.
    }


    ^ This above is a working function I created on my own. With this it will find the first occurence of a substring within a string from the point in the main string you have it start from. It will then return the first position of the first occurence of the substring it can find from your starting point, if it can find it; if it cannot find it from your starting point, then it will return -1.
    - Justin Patrick Butler

    Comme je trouve. "As I find."
    - Butler family quote

    Beneficia sumptos procul superant. "The benefits far exceed the costs."
    - Myself

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    so what?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    is there a question here somewhere or did you mean to post this as an answer to someone else's question?

  4. #4
    jim mcnamara
    Guest
    Adding to Corned Bee's comment - look up strstr()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width