Finding out word occurence without c library calls

myinboxid's Avatar, Join Date: May 2008
Newbie Member
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.
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
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
0
fgpaz13's Avatar, Join Date: May 2008
Newbie Member
Hi myinboxid,

did you manage to write the code?
if so can you share it with us. it would help alot! thanks!
0
mr.anandc's Avatar, Join Date: May 2008
Go4Expert Member
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 shabbir; 15May2008 at 17:12.. Reason: Code block
0
fgpaz13's Avatar, Join Date: May 2008
Newbie Member
thanks a lot mr.anandc..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Please use code block when you have code snippets in posts
0
rebecca_lourdes86's Avatar, Join Date: Jan 2010
Newbie Member
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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[i]==str2[j])" with "if (toupper(str1[i])==toupper(str2[j]))", although that would technically constitute "extracted from other's work"...
0
sahems87's Avatar, Join Date: Jun 2010
Newbie Member
i too got the same question for my technica test...
kindly pls let me know the answer for this question.. plz post the code
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.