C语言 C 中的 strpos - 它是如何工作的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2091825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:11:07  来源:igfitidea点击:

strpos in C- how does it work

c

提问by SuperString

I am really new to C.

我对 C 真的很陌生。

I want to use the strpos function but it is telling me it doesnt exist?

我想使用 strpos 函数,但它告诉我它不存在?

回答by Miere

Here a complete snippet code to solve you problem. PS: Isn't too late to help. ;)

这里有一个完整的代码片段来解决你的问题。 PS:来帮忙还为时不晚。;)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NOT_FOUND -1

int main (){
    int pos = NOT_FOUND;
    if ( (pos = strpos( "subsstring", "string")) != NOT_FOUND )
        printf("found at %d\n", pos);
    else
        printf("not found!\n");
    return 0;
}

int strpos(char *haystack, char *needle)
{
   char *p = strstr(haystack, needle);
   if (p)
      return p - haystack;
   return NOT_FOUND;
}

Edit: Answering Can Vural question:

编辑:回答 Can Vural 问题:

No. I really think that it would be as it is. At structured programming paradigm, it's a common practice to use the scope structure as first parameter on every function that belongs to the structure's scope itself. The strstr function defined at string.h follow the same approach.

不,我真的认为它会是这样。在结构化编程范式中,将作用域结构用作属于结构作用域本身的每个函数的第一个参数是一种常见的做法。string.h 中定义的 strstr 函数遵循相同的方法。

On OOP you have haystack.indexOf( needle ). At structured programming, you have indexOf( haystack, needle ).

在 OOP 上,您有haystack.indexOf( needle ). 在结构化编程中,您拥有indexOf( haystack, needle ).

回答by user231967

The function you are looking for might be either strstr or strchr. You then need to include string.h. There is no strpos in the POSIX interface.

您正在寻找的函数可能是 strstr 或 strchr。然后您需要包含 string.h。POSIX 接口中没有 strpos。

回答by kennytm

Yes. It's called strstr, related to strposlike (pseudo-code):

是的。它被称为strstr,与strpos喜欢(伪代码)相关:

strpos(str, target) {
   res = strstr(str, target); 
   if (res == NULL) return false;
   else             return res - str;
}

回答by Abdulla

I have written strpos() function from scratch with position feature(Like PHP's strpos() function). Return value will be starting position of searched string. Enjoy! :)

我从头开始编写了带有位置特征的 strpos() 函数(如 PHP 的 strpos() 函数)。返回值将是搜索字符串的起始位置。享受!:)

In this example code output will be 12

在此示例代码输出将是 12

#include <stdio.h>
#include <string.h>

int strpos(char *haystack, char *needle, int pos);

int main(){

    printf("%d",strpos("abcdefabcdefabcdef asdfgavcabcddd","abc",10));

    return 0;
}

int strpos(char *haystack, char *needle, int pos){
    int i,j,check,result = -1;
    int len_needle=strlen(needle);
    int len_haystack=strlen(haystack);  
    i = pos;
    if (len_needle>len_haystack || *needle==NULL || i>(len_haystack-1)) return result;

    for(;i<len_haystack;i++){
        check = 0;
        for(j=0;j<len_needle;j++){
            if(haystack[i+j]==needle[j]){
                check++;
            }
        }           
        if(check==len_needle){
            result = i;
            break;
        }

    }
    return result;
}

回答by Mike Anderson

This is in response to Miere and Can Vural. I can't add comments yet so will add this as an answer.

这是对 Miere 和 Can Vural 的回应。我还不能添加评论,所以将其添加为答案。

Shouldn't it be strpos("string", "substring") – Can Vural

At structured programming, you have indexOf( haystack, needle ). Miere

不应该是 strpos("string", "substring") – Can Vural

在结构化编程中,您有 indexOf( haystack, Needle )。米埃

In your code, you have:

在您的代码中,您有:

int strpos(char *haystack, char *needle)

but you also have:

但你也有:

(pos = strpos( "subsstring", "string")) 

I fully agree with the "int strpos(char *haystack, char *needle)" where the string to be searched comes first and the string to search FOR comes second. But to me, "subsstring" (in the context of "one is a substring and one is a string"), "subsstring" implies that IT is the shorter of the two and that you're trying to find "substring" in "string."

我完全同意“int strpos(char *haystack, char *needle)”,其中要搜索的字符串排在第一位,要搜索的字符串排在第二位。但对我来说,“subsstring”(在“一个是子字符串,一个是字符串”的上下文中),“subsstring”意味着 IT 是两者中较短的一个,并且您试图在“细绳。”

So the one part:

所以第一部分:

(pos = strpos( "subsstring", "string")) 

should be:

应该:

(pos = strpos( "string" /*that which is being searched within*/, "substring" /*that which is being searched for in the previous parameter*/)) 

which would be the same as:

这将与:

(pos = strpos( "haystack", "needle")) 

Edit: One of the C comments above wasn't closed properly due to a typo.

编辑:由于打字错误,上面的 C 注释之一没有正确关闭。