Finding out word occurence without c library calls

Discussion in 'C' started by myinboxid, May 3, 2008.

  1. myinboxid

    myinboxid New Member

    Joined:
    May 3, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I got benifited a lot from this forum.Can you please have a look at my requirement and assist me in the same.


    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.

    The function should not make use of any C library function calls.





    Thanks in Advance
    sri.
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    This is just idea make it efficient when you are goin to write coode

    1. Take two pointer ptr1 and ptr2 pointing to haystack and needle respectively.
    2. if contain of ptr1 is equal to contained of ptr2 then
    ++ptr1 and ++ptr2
    3. if ptr2 points to NULL and ptr1 also points to space then
    print " you got that string"
    return
    4. if ptr2 points to NULL and ptr1 not points to space then
    goto step 5
    5. if contained of ptr1 is not equal to contained of ptr2 then
    increment ptr1 untill space come and go to for next string
    reset ptr2 (ptr2 should point to again needle
    6. if contained of ptr1 is not equal to contained of ptr2 and ptr1==NULL
    return -1
     
  3. fgpaz13

    fgpaz13 New Member

    Joined:
    May 14, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi myinboxid,

    did you manage to write the code?
    if so can you share it with us. it would help alot! thanks!
     
  4. mr.anandc

    mr.anandc New Member

    Joined:
    May 6, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Try this......
    Code:
    #include <stdio.h>
    
    int stripos(char *haystack, char *needle)
    {
    char *ptr1, *ptr2, *ptr3;
    
    if( haystack == NULL || needle == NULL)
    return -1;
    
    for (ptr1 = haystack; *ptr1; ptr1++)
    {
    for (ptr2 = ptr1, ptr3 = needle; *ptr3 && (*ptr2 == *ptr3); ptr2++, ptr3++);
    if (!*ptr3)
    return ptr1 - haystack + 1;
    }
    return -1;
    }
    
    int main()
    {
    char str1[] = "Hello world";
    char str2[] = "lo world";
    char str3[] = "Bye";
    printf("location of str2 in str1 is %d\n", stripos(str1, str2));
    printf("location of str3 in str1 is %d\n", stripos(str1, str3));
    return 0;
    }
    
    This is case-sensitive checking... if case-insensitive required, this can be done by changing the (*ptr2 == *ptr3) condition in for loop with a function which compares characters in case-insensitive way.
     
    Last edited by a moderator: May 15, 2008
  5. fgpaz13

    fgpaz13 New Member

    Joined:
    May 14, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    thanks a lot mr.anandc..:D
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please use code block when you have code snippets in posts
     
  7. rebecca_lourdes86

    rebecca_lourdes86 New Member

    Joined:
    Jan 26, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello,

    Please explain to me about this question. I seriously do not understand what this question is asking for. the question is as below:

    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.

    The function should not make use of any C library function calls. The code should be developed entirely by you and not extracted from other's work.
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    It's fairly easy, except I do not know what "offset" is for; the question does not specify this. What it means is that it finds the existence of one string (let's say "lo reb") inside another (let's say "Hello Rebecca"), without considering case. So stripos("Hello Rebecca","lo reb",0) would return 3, and stripos("Hello Rabbit, hello Rebecca","lo reb",0) would return 18. This differs from the standard C library function strstr in that strstr is case sensitive (also iirc strstr returns a char* not an int). So one solution would be to find the source code for strstr() and replace "if (str1==str2[j])" with "if (toupper(str1)==toupper(str2[j]))", although that would technically constitute "extracted from other's work"...
     
  9. sahems87

    sahems87 New Member

    Joined:
    Jun 9, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    i too got the same question for my technica test...
    kindly pls let me know the answer for this question.. plz post the code
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    The idea is that *you* are supposed to write the code. The test is testing your programming skills, not mine.
    Post what you've got so far and explain in detail where you're stuck, and we'll try to unstick you, but we won't do your homework for you.
     
  11. sahems87

    sahems87 New Member

    Joined:
    Jun 9, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.

    The function should not make use of any C library function calls.

    is this code correct or not????

    void main()
    {
    //char str[20]="Your String Goes Here";
    char str[50];
    int chr,i,chr2,flag=0;
    //Or Use This Da
    printf("Enter The String");
    scanf("%s",str);
    printf("Enter The Character To Find");
    scanf("%d",&chr);
    if(chr>=90)
    {
    chr2=chr-32;
    }
    else
    {
    chr2=chr+32;
    }
    for(i=0;i<20;i++)
    {
    if((chr==(int)str)||(chr2==(int)str))
    {
    break;
    flag=1;
    }
    }
    if(flag==1)
    {
    printf("Character Found At position %d ",i-1);
    }
    else
    {
    printf("Character Not Found");
    }
    }
     
  12. sahems87

    sahems87 New Member

    Joined:
    Jun 9, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.

    The function should not make use of any C library function calls.

    is this correct or not??


    void main()
    {
    //char str[20]="Your String Goes Here";
    char str[50];
    int chr,i,chr2,flag=0;
    //Or Use This Da
    printf("Enter The String");
    scanf("%s",str);
    printf("Enter The Character To Find");
    scanf("%d",&chr);
    if(chr>=90)
    {
    chr2=chr-32;
    }
    else
    {
    chr2=chr+32;
    }
    for(i=0;i<20;i++)
    {
    if((chr==(int)str)||(chr2==(int)str))
    {
    break;
    flag=1;
    }
    }
    if(flag==1)
    {
    printf("Character Found At position %d ",i-1);
    }
    else
    {
    printf("Character Not Found");
    }
    }
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Please use code tags when posting code. It's right there in the posting guidelines.

    Why do you need to ask if it's correct?
    Does it work as you expect? If not then how can it be correct? If it does then how can it not be correct? (OK it might not be optimal, but if it works then that's often good enough)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice