strstr

Implementing strnstr

南笙酒味 提交于 2019-11-30 13:58:02
问题 I am trying to implement a strnstr function into C (strstr but it checks the length), for some reason it doesn't work (output is always no): #include <stdio.h> char *searchingFor = "stackdummy"; char *in = "la da\ndoo a da\nnow here comes the stack\nok there it was.\n"; char *strnstr(char *s1, char *s2, int length) { if(s1 == NULL || s2 == NULL) return NULL; printf("searching \n\n\"%s\"\n for %.*s\n", s1, length, s2); char *ss1 = malloc(strlen(s1) + 1); strcpy(ss1, s1); char *ss2 = malloc

strstr faster than algorithms?

落爺英雄遲暮 提交于 2019-11-30 10:48:42
问题 I have a file that's 21056 bytes. I've written a program in C that reads the entire file into a buffer, and then uses multiple search algorithms to search the file for a token that's 82 chars. I've used all the implementations of the algorithms from the “Exact String Matching Algorithms” page. I've used: KMP, BM, TBM, and Horspool. And then I used strstr and benchmarked each one. What I'm wondering is, each time the strstr outperforms all the other algorithms. The only one that is faster

Implementing strnstr

怎甘沉沦 提交于 2019-11-30 09:15:11
I am trying to implement a strnstr function into C (strstr but it checks the length), for some reason it doesn't work (output is always no): #include <stdio.h> char *searchingFor = "stackdummy"; char *in = "la da\ndoo a da\nnow here comes the stack\nok there it was.\n"; char *strnstr(char *s1, char *s2, int length) { if(s1 == NULL || s2 == NULL) return NULL; printf("searching \n\n\"%s\"\n for %.*s\n", s1, length, s2); char *ss1 = malloc(strlen(s1) + 1); strcpy(ss1, s1); char *ss2 = malloc(length + 1); strncpy(ss2, s2, length); char *result = strstr(ss1, ss2); free(ss1); free(ss2); return

Optimized version of strstr (search has constant length)

♀尐吖头ヾ 提交于 2019-11-30 08:53:57
My C program had a lot of strstr function calls. The standard library strstr is already fast but in my case the search string has always length of 5 characters. I replaced it with a special version to gain some speed: int strstr5(const char *cs, const char *ct) { while (cs[4]) { if (cs[0] == ct[0] && cs[1] == ct[1] && cs[2] == ct[2] && cs[3] == ct[3] && cs[4] == ct[4]) return 1; cs++; } return 0; } The function returns an integer because it’s enough to know if ct occurs in cs. My function is simple and faster than standard strstr in this special case but I’m interested to hear if anybody has

strstr faster than algorithms?

限于喜欢 提交于 2019-11-29 22:30:42
I have a file that's 21056 bytes. I've written a program in C that reads the entire file into a buffer, and then uses multiple search algorithms to search the file for a token that's 82 chars. I've used all the implementations of the algorithms from the “Exact String Matching Algorithms” page. I've used: KMP, BM, TBM, and Horspool. And then I used strstr and benchmarked each one. What I'm wondering is, each time the strstr outperforms all the other algorithms. The only one that is faster sometimes is BM. Shouldn't strstr be the slowest? Here's my benchmark code with an example of benchmarking

Optimized version of strstr (search has constant length)

北慕城南 提交于 2019-11-29 12:03:22
问题 My C program had a lot of strstr function calls. The standard library strstr is already fast but in my case the search string has always length of 5 characters. I replaced it with a special version to gain some speed: int strstr5(const char *cs, const char *ct) { while (cs[4]) { if (cs[0] == ct[0] && cs[1] == ct[1] && cs[2] == ct[2] && cs[3] == ct[3] && cs[4] == ct[4]) return 1; cs++; } return 0; } The function returns an integer because it’s enough to know if ct occurs in cs. My function is

JavaScript or jQuery equivalent of PHP's strstr() function

最后都变了- 提交于 2019-11-27 15:32:48
Is there a function in jQuery or JavaScript that does the same as strstr() in PHP? I have an AJAX response that should be 1,2,3,12,13,23 or 123. I want to check if 1 exists, then if 2 exists then if 3 exists. Andrew Lee Try using this: function strstr(haystack, needle, bool) { // Finds first occurrence of a string within another // // version: 1103.1210 // discuss at: http://phpjs.org/functions/strstr // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Onno Marsman // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: strstr(

Which method is preferred strstr or strpos?

感情迁移 提交于 2019-11-27 07:43:40
I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ? From the PHP online manual : If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead. Sk8erPeter Here are some other answers (+benchmarks) I got to my question, which is almost the same (I didn't realize yours when asking). In the meantime I also made my own benchmark test, which I ran 1000000 times for each relevant functions ( strstr() , strpos() , stristr() and stripos() )

strstr not functioning

僤鯓⒐⒋嵵緔 提交于 2019-11-27 05:12:23
Why does this particular piece of code return false on the strstr() if I input "test"? char input[100]; int main() { fgets(input, 100, stdin); printf("%s", input); if(strstr("test message", input)) { printf("strstr true"); } } I thought strstr searched the first param for instances of the second param? It works when I replace input with some text or just assign it something directly, but it seems to not work with fgets. It's because fgets stores the newline character so when strstr does a comparison it fails. From the man page: fgets() reads in at most one less than size characters from stream

JavaScript or jQuery equivalent of PHP's strstr() function

北战南征 提交于 2019-11-26 17:12:54
问题 Is there a function in jQuery or JavaScript that does the same as strstr() in PHP? I have an AJAX response that should be 1,2,3,12,13,23 or 123. I want to check if 1 exists, then if 2 exists then if 3 exists. 回答1: Try using this: function strstr(haystack, needle, bool) { // Finds first occurrence of a string within another // // version: 1103.1210 // discuss at: http://phpjs.org/functions/strstr // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Onno