簡體   English   中英

通過 function c 傳遞帶有指針的字符串

[英]passing string with pointer through function c

我,我正在嘗試編寫此代碼,它應該計算不包括在字符串中的 substring 的數量,例如(如下),主要是我嘗試使用指針在不使用 arrays 的情況下使用字符串,但它沒有用完全沒有!!

// count_target_string("abc of", "of") -> 1
// count_target_string("abcof", "of")  -> 0
// count_target_string("abc of abc of", "of") -> 2 


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int countTargetString(char* text , char* string){
        
        char d[]=" ";
        char * portion = strtok(text,d);
        int result=0;
        while (portion!=NULL){
            if (strcmp(portion,string)==0){
                result++;
            }
            portion = strtok(NULL,d);
        }
        return result;
    }
    
    
    int main(){
       
        printf("%d\n",countTargetString("abc of abc of","of"));
     
         char *test ="abc of abc of";
         char *d = "of";
         printf("%d\n",countTargetString(test,d));
    
        return 0;
    }
  1. strtok修改字符串。
  2. char *test ="abc of abc of"; 定義指向字符串字面量的指針。 修改字符串文字會調用未定義行為 (UB)。 這就是為什么您的代碼“根本不起作用”如果您將字符串文字引用直接傳遞給 function (即使用字符串文字作為參數) countTargetString("abc of abc of","of")); .

您的指針必須引用可修改的字符串:

int main()
{
    char mystring[] = "abc of abc of";
    char *test = mystring;
    char *d = "of";
    printf("%d\n",countTargetString(test,d));
}

在 function countTargetString的兩個調用中

 printf("%d\n",countTargetString("abc of abc of","of"));
 
 char *test ="abc of abc of";
 char *d = "of";
 printf("%d\n",countTargetString(test,d));

您正在傳遞指向字符串文字的指針。

盡管在 C 中與 C++ 字符串文字具有非常量字符類型 arrays 相對,但您可能不會更改字符串文字。 任何更改字符串文字的嘗試都會導致未定義的行為。

來自 C 標准(6.4.5 字符串文字)

7 不確定這些 arrays 是否不同,只要它們的元素具有適當的值。 如果程序嘗試修改這樣的數組,則行為未定義。

function strtok更改插入終止零字符 '\0' 的源字符串以提取子字符串。

即使在 C 中,使用限定符const聲明指向字符串文字的指針也總是更好。

您可以使用 function strstr而不是 function strtok

這是一個演示程序。

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

size_t countTargetString( const char *s1, const char *s2 )
{
    size_t count = 0;
    size_t n = strlen( s2 );

    for ( const char *p = s1; ( p = strstr( p, s2 ) ) != NULL; p += n )
    {
        if ( ( p == s1 || isblank( ( unsigned char )p[-1] ) ) &&
             ( p[n] == '\0' || isblank( ( unsigned char )p[n] ) ) )
        {
            ++count;
        }
    }

    return count;
}

int main( void )
{
     printf("%zu\n",countTargetString("abc of abc of","of"));
     
     const char *test ="abc of abc of";
     const char *d = "of";

     printf("%zu\n",countTargetString(test,d));
}

程序 output 是

2
2

如您所見,function 參數也使用限定符const聲明,因為 function 不會更改傳遞的字符串。

請注意,無論如何要計算字符串中子字符串的出現次數,更改原始字符串是一個壞主意。

雖然strtok不適用於字符串文字,但可以使用strspnstrcspn

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

int countTargetString(char* text , char* string){
    char d[]=" ";
    int result = 0;
    size_t span = 0;
    while ( *text) {
        text += strspn ( text, d);
        span = strcspn ( text, d);
        if ( strncmp ( text, string, span)) {
            ++result;
        }
        text += span;
    }
    return result;
}


int main( void) {

    printf("%d\n",countTargetString("abc of abc of","of"));

     char *test ="abc of abc of";
     char *d = "of";
     printf("%d\n",countTargetString(test,d));

    return 0;
}
int count_substr(const char* target, const char* searched) {
    int found = 0;
    unsigned long s_len = strlen(searched);
    for (int i = 0; target[i]; i++) {
        // use memcmp to NOT compare the null terminator of searched
        if (memcmp(target + i, searched, s_len) == 0) {
            found++;
            i += s_len - 1;
        }
    }
    return found;
}

這是 substring 計數的一個非常基本的實現。 為了獲得最快的解決方案,請從 wikipedia 或任何您想要的地方復制 boyer moore 模式匹配算法,並將其修改為匹配而不是終止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM