strstr

Recreate the strstr() function

风流意气都作罢 提交于 2021-02-07 09:38:47
问题 Hello i am trying to make my own strstr() function and i can't figure out why it is returning a segmentation fault.I am trying to search a string within another string and then return a pointer to the first 'same' letter. Any help would be appreciated. This is my code: char* ms_search(char *Str1,char* Str2){ char* p = NULL; int i,k=0,j = 0; for(i = 0;i < ms_length(Str1); i++){ if(Str1[i] == Str2[k]){ if(k == 0){ p = &Str1[i]; j= i; } if(k == ms_length(Str2)){ break; } k++; } else{ if(Str1[i]

Finding the substring in an input string

谁说我不能喝 提交于 2021-01-27 23:12:38
问题 I have this assembly program where I need to find the substring in the main string I input. My problem is that it always outputs the "word found" even if I typed two completely different words. I don't know which part of my loop or condition is wrong. Please help me figure it out. Also, please suggest some string instructions that could be used in checking for a substring so that I can shorten my code. I am really confused with how the cmpsb works, I only tried to use it. Btw, I don't know

Matching an exact word using in c

末鹿安然 提交于 2020-05-29 06:56:27
问题 Can I use the strstr function to match exact word? For example, let's say I have the word hello , and an input string line : if char* line = "hellodarkness my old friend"; and I use result = strstr(line, "hello"); result will match (be not NULL), however I want to match only the exact word "hello" (so that "hellodarkness" would not match) and result will be NULL. Is it possible to do this using strstr or do I have to use fscan and scan the line word by word and check for matches? 回答1: I would

strstr works only if my substring is at the end of string

对着背影说爱祢 提交于 2020-01-11 14:46:46
问题 I've encountered a couple of problems with the program that i'm writing now. strstr outputs my substring only if it's at the end of my string it also outputs some trash characters after that I've had problems with "const char *haystack" and then adding input to it, so i did it with fgets and getchar loop somewhere along the way it worked with a substring that was not only at the end, but then i outputted substring and the rest of the string ater that here is my main: int main() { char

Does strstr()'s implementation in gcc and VS have linear complexity?

强颜欢笑 提交于 2020-01-11 09:21:25
问题 I know that there are fast string search algorithms, like Boyer–Moore and Knuth–Morris–Pratt, that have O(n+m) complexity, while the trivial solution would be O(n*m). So does the implementation of strstr() for the most popular toolchains - gcc and Visual Studio - use these fast O(n) algorithms, or does it use the trivial solution? 回答1: GCC's runtime library uses Two-Way Algorithm which performs 2n-m text character comparisons in the worst case. It is O(n) complexity in the search phase but it

strstr() function

谁说胖子不能爱 提交于 2020-01-02 03:37:09
问题 I am trying to write a program that compares a substring that the user enters with an array of strings. #include <stdio.h> #include <string.h> char animals[][20] = { "dogs are cool", "frogs are freaky", "monkeys are crazy" }; int main() { char input[10]; puts("Enter animal name: "); fgets(input, sizeof(input), stdin); int i; for(i = 0; i < 3; i++) { if(strstr(animals[i], input)) printf("%s", animals[i]); } return 0; } When I enter frogs, for example it should print the message "frogs are

strstr php or alternative part string

两盒软妹~` 提交于 2019-12-24 09:37:02
问题 At the moment I'm using strstr() for getting the first part of a string. I have my project running on 3 servers. 1. development server 2. test server 3. live server the strstr() works fine on the development server and test server, but the moment I place my project live, the strstr prints nothing. for example: I use this code: //$product["titel2"] = "apple - &#60fruit&#60"; $product["titel2"]=(strstr($product["titel2"], "&#60domino"))?strstr($product["titel2"], "&#60fruit",true):$product[

strstr to show string before occurance

醉酒当歌 提交于 2019-12-21 19:53:34
问题 I want to get the first bit of the string after the occurrence of the needle like this... $user = strstr('someemail@yahoo.com', '@', true); But this only works with PHP Version 5.3.0, I have 5.2.9 Is there a way I can get the same results? 回答1: list($user, $therest) = explode('@',$email); 来源: https://stackoverflow.com/questions/1521898/strstr-to-show-string-before-occurance

How to find text between two strings in c

浪尽此生 提交于 2019-12-20 15:34:11
问题 I need to extract the text between 2 string patterns in c. Example: aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa PATTERN1=<BBBB> PATTERN2=</BBBB> Thanks. 回答1: Here is an alive example of how to do this #include <stdio.h> #include <string.h> int main(void) { const char *s = "aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa"; const char *PATTERN1 = "<BBBB>"; const char *PATTERN2 = "</BBBB>"; char *target = NULL; char *start, *end; if ( start = strstr( s, PATTERN1 ) ) { start += strlen( PATTERN1 ); if

How to find text between two strings in c

*爱你&永不变心* 提交于 2019-12-20 15:34:09
问题 I need to extract the text between 2 string patterns in c. Example: aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa PATTERN1=<BBBB> PATTERN2=</BBBB> Thanks. 回答1: Here is an alive example of how to do this #include <stdio.h> #include <string.h> int main(void) { const char *s = "aaaaaa<BBBB>TEXT TO EXTRACT</BBBB>aaaaaaaaa"; const char *PATTERN1 = "<BBBB>"; const char *PATTERN2 = "</BBBB>"; char *target = NULL; char *start, *end; if ( start = strstr( s, PATTERN1 ) ) { start += strlen( PATTERN1 ); if