Question:
C programming language?
Bear
2010-09-07 07:38:53 UTC
Does anybody know how to solve this?

Write the following function in C.

stripos — Find position of first occurrence of a case-insensitive string
int stripos ( char* haystack, char* needle, int offset )

Returns the numeric position of the first occurrence of needle in the
haystack string. Note that the needle may be a string of one or more
characters. If needle is not found, stripos() will return -1.
Four answers:
?
2010-09-07 11:19:09 UTC
Maybe something like this?

#include

#include

#include

#include



char *make_upper(char *szString)

{

char *szRet = strdup(szString);



int iLen = strlen(szRet);

int i = 0;

for (i = 0; i < iLen; i++){

szRet[i] = toupper(szRet[i]);

}

return szRet;

}



int stripos ( char* haystack, char* needle, int offset )

{

int ret = -1;

int i = 0,

iLen = strlen(haystack),

iNeedle = strlen(needle);

char *szSearch = make_upper(haystack);

char *szNeedle = make_upper(needle);



if (iLen > offset) {

for (i = offset; i < iLen; i++){

if (!strncmp(&szSearch[i], szNeedle, iNeedle)){

ret = i;

break;

}

}

}

free(szSearch);

free(szNeedle);

return ret;

}



int main()

{

char szHaystack[] = "The fox jumps over the brown meshed fences with joy",

szNeedle[] = "Fence";

int iPos = stripos(szHaystack, szNeedle, 0);

if (iPos != -1){

printf("Sentences: [%s]\n"

"Search Expression: [%s]\n"

"Found Expression: [%*.*s]\n"

"Offset: %d\n",

szHaystack, szNeedle, strlen(szNeedle), strlen(szNeedle), &szHaystack[iPos], iPos);

}

return 0;

}
cja
2010-09-07 09:30:01 UTC
It's interesting how some questions keep coming back to Y!A; in this case, verbatim, 1 year ago and 2 years ago. So, you could search and find it, but I'll give it you again here. To make it more interesting, I'll assume you're not allowed to use the library function strstr, and you have to code it yourself. Your code should look something like this:



#include

#include

#include

#include



int stripos(const char * const haystack,

                        const char * const needle,

                        int offset);

char *_strstr(char *string, char *substring);





#define HAYSTACK "The quick brown fox jumped over the lazy dog"

#define NEEDLE0 "The"

#define NEEDLE1 "Brown"

#define NEEDLE2 "lazy_dog"



void find(char *s1, char *s2, int offset);



int main(int argc, char *argv[]) {

    find(HAYSTACK,NEEDLE0,0);

    find(HAYSTACK,NEEDLE0,1);

    find(HAYSTACK,NEEDLE1,0);

    find(HAYSTACK,NEEDLE1,6);

    find(HAYSTACK,NEEDLE2,20);

    find(HAYSTACK,NEEDLE2,50);

    find("",NEEDLE0,0);

    return 0;

}



void find(char *s1, char *s2, int offset) {

    int pos = stripos(s1,s2,offset);

    printf("\ns1: %s (offset = %d)",s1,offset);

    printf("\ns2: %s ... ",s2);

    if (pos == -1) printf("not found");

    else printf("found at pos = %d",pos);

    puts("");

}



int stripos(const char * const haystack,

                        const char * const needle,

                        int offset) {

    int pos = -1;

    if ((haystack != NULL) && (needle != NULL) &&

              (offset < strlen(haystack))) {

        char *s1 = (char *)malloc(strlen(haystack)+1);

        char *s2 = (char *)malloc(strlen(needle)+1);

        char *p;



        strcpy(s1,haystack);

        strcpy(s2,needle);

        for (p = s1; *p != '\0'; p++) *p = tolower((int)*p);

        for (p = s2; *p != '\0'; p++) *p = tolower((int)*p);

        if ((p = _strstr(s1+offset,s2)) != NULL) {

            pos = p - s1;

        }

        free(s1);

        free(s2);

    }

    return pos;

}



char *_strstr(char *string, char *substring) {

    char *p = NULL, *q = string, *z = substring;



    if ((z != NULL) && (q != NULL)) {

        while ((*q != '\0') && (*z != *q)) {

            q++;

        }

        if (*q != '\0') {

            p = q;

        }

        while ((*q != '\0') && (*z != '\0') && (*q == *z)) {

            q++; z++;

        }

        if (*z != '\0') p = NULL;

    }

    return p;

}



#if 0



Program output:



s1: The quick brown fox jumped over the lazy dog (offset = 0)

s2: The ... found at pos = 0



s1: The quick brown fox jumped over the lazy dog (offset = 1)

s2: The ... found at pos = 32



s1: The quick brown fox jumped over the lazy dog (offset = 0)

s2: Brown ... found at pos = 10



s1: The quick brown fox jumped over the lazy dog (offset = 6)

s2: Brown ... found at pos = 10



s1: The quick brown fox jumped over the lazy dog (offset = 20)

s2: lazy_dog ... not found



s1: The quick brown fox jumped over the lazy dog (offset = 50)

s2: lazy_dog ... not found



s1: (offset = 0)

s2: The ... not found



#endif
Chris C
2010-09-07 08:57:44 UTC
1) I would make a copy of haystack, and needle. The haystack copy would only be a copy after the 'offset' characters: strcpy(haystack_copy, haystack + offset); strcpy(needle_copy, needle);

2) Then I would uppercase both copies (haystack_copy and needle_copy).

3) Then I would call "strstr(haystack_copy, needle_copy);" and if the return was NULL return -1, otherwise return the value that was returned minus the beginning: pos = strstr(haystack_copy, needle_copy); return (pos == NULL ? -1 : (haystack_copy - pos) + offset);



I'll leave you to code the strupr() function mentioned in #2 above.
George H
2010-09-07 07:43:39 UTC
Start by assigning your input string to a variable.

Store the length of the string in another variable.

Start a for loop, starting from 0 to the length of the string.

In every iteration of the loop, check for the needle character. If it is found, return the current iteration number (your 'n' or 'i').

If it is not found, the code will execute passed the loop, at which point you can return -1.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...