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 7506167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:10:17+00:00 2026-05-29T22:10:17+00:00

I am trying to write the strstr function from scratch. I went through my

  • 0

I am trying to write the strstr function from scratch. I went through my code line by line in the debugger and it works fine. However, it is not saving the start of the substring its searching for properly. And, therefore, it is not returning it properly. I do not have much programming experience so my code is a little messy and convoluted. However, it does work for the most part. Here is my code below (commented for my professor and for you all to see what I did). (also, my professor has already expressed his acceptance of the goto function)

char *strgstr(const char *str1, const char *str2)
{
    //I went through this function line by line with the debugger
    //The only problem with it is when I go to save the location of the
    //substring in str1.  
    //I posted a question on stackoverflow and I was able to get it to compile
    //but it still doesn't save and return properly.  The rest of the function works.
    int len_str1=strlen(str1);
    int len_str2=strlen(str2);
    char *save_str=NULL;
    int i=0;
    for(; i<len_str1; i++)
    {
there:
        if(str1[i]==str2[0])            //checks if this is the beginning of str2
        {
            save_str=(char*)str1[i];    //This is where the problem is. 
            int j=0;                    //start at beginning of str2
            for(;i<len_str1;i++)        //index str1
            {
                if(j<len_str2)          //checks if we've finished searching str2
                {
                    if(str1[i]!=str2[j])
                    {
                        goto there;
                    }
                    j++;
                }
                else
                {
                    return save_str;    //does not return properly.  I can't figure out how to save a particular point in the index to a pointer. 
                }
            }
        }

    }
}
  • 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-29T22:10:18+00:00Added an answer on May 29, 2026 at 10:10 pm

    The line you’ve written as

    save_str=(char*)str1[i];    //This is where the problem is. 
    

    should be (for example)

    save_str = str1 + i;   
    

    Your original version is treating the numerical value of the character as a pointer, which is completely wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a code that can the set property value through the lambda
im trying to write an app that will display a list off lines from
Im trying to write a function for adding category: function addCategory() { $cname =
I'm trying to write a regex function that will identify and replace a single
I'm trying to write a blog post which includes a code segment inside a
Trying to write a code that searches hash values for specific string's (input by
Im trying to write a recursive function which returns the factorial of a number.
I'm trying write a simple perl script that reads some fields from a password
Trying to write a python application that downloads images from an RSS feed, and
Trying to write a matrix-multiplying function for arbitrary-sized matrices in C. I'm trying the

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.