Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:21:41+00:00 2026-05-27T18:21:41+00:00

How do I do the in-place equivalent of strstr() for a counted string (i.e.

  • 0

How do I do the in-place equivalent of strstr() for a counted string (i.e. not null-terminated) in C?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:21:42+00:00Added an answer on May 27, 2026 at 6:21 pm

    If you’re afraid of O(m*n) behaviour – basically, you needn’t, such cases don’t occur naturally – here’s a KMP implementation I had lying around which I’ve modified to take the length of the haystack. Also a wrapper. If you want to do repeated searches, write your own and reuse the borders array.

    No guarantees for bug-freeness, but it seems to still work.

    int *kmp_borders(char *needle, size_t nlen){
        if (!needle) return NULL;
        int i, j, *borders = malloc((nlen+1)*sizeof(*borders));
        if (!borders) return NULL;
        i = 0;
        j = -1;
        borders[i] = j;
        while((size_t)i < nlen){
            while(j >= 0 && needle[i] != needle[j]){
                j = borders[j];
            }
            ++i;
            ++j;
            borders[i] = j;
        }
        return borders;
    }
    
    char *kmp_search(char *haystack, size_t haylen, char *needle, size_t nlen, int *borders){
        size_t max_index = haylen-nlen, i = 0, j = 0;
        while(i <= max_index){
            while(j < nlen && *haystack && needle[j] == *haystack){
                ++j;
                ++haystack;
            }
            if (j == nlen){
                return haystack-nlen;
            }
            if (!(*haystack)){
                return NULL;
            }
            if (j == 0){
                ++haystack;
                ++i;
            } else {
                do{
                    i += j - (size_t)borders[j];
                    j = borders[j];
                }while(j > 0 && needle[j] != *haystack);
            }
        }
        return NULL;
    }
    
    char *sstrnstr(char *haystack, char *needle, size_t haylen){
        if (!haystack || !needle){
            return NULL;
        }
        size_t nlen = strlen(needle);
        if (haylen < nlen){
            return NULL;
        }
        int *borders = kmp_borders(needle, nlen);
        if (!borders){
            return NULL;
        }
        char *match = kmp_search(haystack, haylen, needle, nlen, borders);
        free(borders);
        return match;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently discovered that MB/s is technically equivalent to 8 million bits/s and not
Possible Duplicate: Implementing a no-op std::ostream Is there any stream equivalent of NULL in
My place of employment is looking into buying a third party tool, for batch
I place using namespace in a view code behind but i can't call any
My place of work currently uses CVS. A git migration is planned but it
Every place I've worked in the past 15 years has had, on almost every
I'm trying to place this menu on the left hand side of the page:
Where is a good place to start with making an application in .NET that
Is there a place to find all possible uses of the syscmd method in
When I place a control on a tabpage in Silverlight the control is placed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.