2016-02-16 23 views
-4

讓我說我有字符串「我擁有許多寵物」。 如何在C語言中的「many」之前找到字符串的長度?我需要找到一個子串的長度beore一個字在C

+0

你看看[類似的問題(https://stackoverflow.com/search?q= [C] +找到+子)?答案應該至少能讓你獲得部分解決方案。 – 5gon12eder

+0

可能重複的[如何從字符串查找子串?](http://stackoverflow.com/questions/13195353/how-to-find-substring-from-string) – user1803551

回答

2

只要找到第一次出現的索引「多」

可以使用strstr()功能做到這一點,只是找到strstr()返回的指針和指向整個字符串之間的區別。

正如@ 4386427指出的那樣,如果我們談論單詞(單詞≠子串),此解決方案可能不起作用。

所以,如果你想用單詞做些什麼,你將不得不檢查你的子字符串前後是否有空格字符(並處理這個單詞在字符串開頭或結尾的情況搜索中的子字符串)。

+0

可以請你詳細說明嗎? – thedude

+0

'他的臉比較小' - 找到單詞'is'前面的長度....也許'strstr'alone不是解決方案 – 4386427

+2

@thedude你到目前爲止做了什麼,你有什麼不明白的? – vmonteco

0

我想用另一個字替換字符串中的一個字,但該字 不必是相同的長度。我可以做到這一點,如果他們是相同的長度,但我不知道如何做,如果他們是不同的長度。我 雖然只拼接在一起有字符串來做到這一點。但我是 不知道該怎麼做。

查找字符串中的每個單詞使用strstr(mystr, " ");假設只有一個字與字之間的空間(只是爲了簡化分析),創建使用startend while循環。每個單詞從start開始,並且在結束指針中的單詞後面有空白。使用此循環設置指針陣列

/* This assumes there is only one space between words 
    It also assumes there are no leading or trailing blanks 
    The assumptions are just for ease of analysis. 
*/ 
char str[] ="This is a simple string"; 
char *start = str; 
char *end = NULL; 
int count = 0; 
char mystring[MAX_STRINGS][MAX_SIZE]; 
while(end = strstr(start," ")) 
{ 
    len = end - start; 
    // copy the substring into the array using strncpy() 
    strncpy(mystring[count], start, len); 
    // add the final '\0' character to the string in the array 
    mystring[count++][len] = '\0'; 
    start = end + 1; 
} 
// Copy the last substring int the array 
// end is now NULL but the substring ends with '\0' 
// Thus a plain strcpy() is all that is required. 
strcpy(mystring[count], start); 

您應該將單詞列表設置爲指針數組。我要把這作爲一組的步驟,而不是代碼,以便您可以構建代碼yoursel

  1. 創建數組的指針爲字符串。

    char * mystrings [MAX_STRINGS];

  2. 將大的句子字符串拆分爲單個單詞並將每個單詞放入單獨的字符串中。您可以使用malloc來創建適當大小的緩衝區。

  3. 確定具有要替換的單詞的指針。

  4. 創建一個指向新詞,並把它放入數組

  5. 釋放舊指針。

  6. 使用strcat()將數組中的字符串連接成新字符串。

  7. 免費的指針,並確保你不會溢出最後的緩衝,當你做了strcat()

另一種方法是創建一個二維字符數組。

char mystrings[MAX_STRINGS][MAX_SIZE]; 

使用類似的過程,除了使用strcpy()的話複製到緩衝區。舊字的替換也通過strcpy()完成,您不必釋放舊指針。再次使用strcat()將新句子作爲字符串。請注意,由於strcpy()將最終的NUL字符置於原地,因此只要不超過緩衝區,這兩個字的大小就不一樣了。只要不超出緩衝區,Similarlt strcat()會將最終的NUL放置到位以產生結果字符串。

0

一種方法是首先檢查是否找到sub string,如果找到,則打印其位置-1

嘗試以下操作:

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

void checkString(char *string1, char *string2); 

int main(void){ 
    char test[256] = "I own many pets"; 
    char string2[] = "many"; 

    checkString(test, string2); 
    return 0; 
} 

void checkString(char *string1, char *string2){ 
    char *s1, *s2, *s3; 

    size_t lenstring1 = strlen(string1); 
    size_t lenstring2 = strlen(string2); 

    if (lenstring2 < 1){ 
     printf("There is no substring found"); 
     exit(1); 
    } 

    size_t i=0,j=0; 
    int found=0; 

    s1 = string1; 
    s2 = string2; 


    for(i = 0; i < lenstring1; i++){ 
     if(*s1 == *s2){ 
      s3 = s1; 
      for(j = 0;j < lenstring2;j++){ 
       if(*s3 == *s2){ 
        s3++;s2++; 
       }else{ 
        break; 
       } 
      } 

      s2 = string2; 
      if(j == strlen(string2)){ 
       found = 1; 
       printf("The length is %zu\n", i); 
       } 
      } 
     s1++; 
    } 

    if(found == 0){ 
     printf("No match Found"); 
    } 
} 

輸出:

The length is 6 
0
char *replace(char *str, char *old, char *new) 
{ 
    char *ret; 
    int lold, lnew; 
    char *pos = strstr(str, old); 

    if(pos == NULL) 
     return NULL; 

    lnew = strlen(new); 
    lold = strlen(old); 
    ret = malloc(1+strlen(str)+lnew-lold); 
    if(pos != str) 
     strncpy(ret, str, pos-str); 
    strcat(ret, new); 
    strcat(ret, pos+lold); 
    return ret; 
}