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.
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
Hi myinboxid, did you manage to write the code? if so can you share it with us. it would help alot! thanks!
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.
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.
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"...
i too got the same question for my technica test... kindly pls let me know the answer for this question.. plz post the code
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.
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"); } }
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"); } }
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)