سؤال

هل هناك مكتبة C وظيفة من شأنها أن عودة مؤشر شخصية في سلسلة ؟

حتى الآن, كل ما وجدته هي وظائف مثل strstr أنه سيعود وجدت شار * ليس مكانها في سلسلة الأصلي.

هل كانت مفيدة؟

المحلول

أعتقد أن

size_t strcspn ( const char * str1, const char * str2 );

هو ما تريد.هنا هو مثال على سحبها من هنا:

/* strcspn example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "fcba73";
  char keys[] = "1234567890";
  int i;
  i = strcspn (str,keys);
  printf ("The first number in str is at position %d.\n",i+1);
  return 0;
}

نصائح أخرى

strstr عودة المؤشر إلى العثور على حرف ، لذلك يمكن استخدام مؤشر الحساب:(ملاحظة:هذا الرمز ليس اختبار لقدرته على تجميع انه على بعد خطوة واحدة من شبة الكود.)

char * source = "test string";         /* assume source address is */
                                       /* 0x10 for example */
char * found = strstr( source, "in" ); /* should return 0x18 */
if (found != NULL)                     /* strstr returns NULL if item not found */
{
  int index = found - source;          /* index is 8 */
                                       /* source[8] gets you "i" */
}

تحرير:strchr هو أفضل واحد فقط شار.مؤشر aritmetics يقول "مرحبا!":

char *pos = strchr (myString, '#');
int pos = pos ? pos - myString : -1;

المهم: strchr () بإرجاع NULL إذا لم السلسلة وجدت

يمكنك استخدام strstr لإنجاز ما تريد.على سبيل المثال:

char *a = "Hello World!";
char *b = strstr(a, "World");

int position = b - a;

printf("the offset is %i\n", position);

وتنتج هذه النتيجة:

the offset is 6

إذا كنت غير مرتبطة تماما النقي ج و يمكن استخدام سلسلة.ح هناك strchr() انظر هنا

الكتابة الخاصة بك :)

رمز من BSD مرخصة معالجة سلسلة مكتبة C, يسمى zString

https://github.com/fnoyanisi/zString

int zstring_search_chr(char *token,char s){
    if (!token || s=='\0')
        return 0;

    for (;*token; token++)
        if (*token == s)
            return 1;

    return 0;
}

يمكنك كتابة

s="bvbrburbhlkvp";
int index=strstr(&s,"h")-&s;

للعثور على مؤشر 'h' في تشويش.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top