0

I am new to programming in C and am trying to write a simple function that will compare strings. I am coming from java so I apologize if I'm making mistakes that seem simple. I have the following code:

/* check if a query string ps (of length k) appears 
     in ts (of length n) as a substring 
     If so, return 1. Else return 0
     */
int
simple_substr_match(const unsigned char *ps,    /* the query string */
                         int k,                     /* the length of the query string */
                         const unsigned char *ts,   /* the document string (Y) */ 
                         int n                      /* the length of the document Y */)
{
    int i;
    for(i = 0;i < n;i+k){
        char comp;
        comp = ts->substring(i,k);
        if (strncmp(comp, ps, k)) {
        return 1;
        }
    }
    return 0;
}

When trying to compile i get the error: request for member 'substring' in something not a structure or union.

The idea of the code is describe in the code comment but just to elaborate I am looking to see if ps occurs as a substring of ts in increments of k(length of ps).

What am I doing wrong and how can I fix it? Is there a better way of doing what I am trying to do?

4
  • 3
    C != C++. And C doesn't have a substring method. Commented Feb 16, 2014 at 21:44
  • What language is this? Commented Feb 16, 2014 at 21:46
  • 5
    No need to downvote imo. Yes, the code is wrong, and finding the bugs isn't rocket science, but he said he's new, he tried, provided a code example, and posted the error message. That's way more than many do. Commented Feb 16, 2014 at 21:48
  • Im coming from java so I apologize if Im making mistakes that may seem simple. I am trying to transition from java to c. Commented Feb 16, 2014 at 23:03

4 Answers 4

2

Change

for(i = 0;i < n;i+k){
    char comp;
    comp = ts->substring(i,k);
    if (strncmp(comp, ps, k)) {
    return 1;
    }
}

to

for(i = 0;i < n-k;i++){
if (!strncmp(ts+i, ps, k)) {
        return 1;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help this eliminated the error I was getting but the code does not same to be accomplishing what I want. I am trying to look for ps as a substring of ts in k increments of ts. How can I modify this code to get that result?
2

ts is a char* not a class (and you're writing in C, not C++)

How about using the standard 'strstr' C function?

if (strstr(ts, ps) != NULL) {
  return 1;
} else {
  return 0;
}

Comments

0

C doesn't have member functions. and in c++ char doesn't have a member function substring. you should use ts as an character array.

Comments

0

Something like this?

#define simple_substr_match(ps,ts) (strstr(ts,ps) != NULL)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.