2015-04-05 170 views
0

所以我可能會攻擊這個錯誤的方式;我正在學習一些Javascript,並編寫了一個程序,該程序將查找另一個字符串中包含的字符串,並將匹配存儲在一個數組中(如果沒有太大意義,請參閱我的程序應該幫助)。我試圖將我的程序翻譯爲C,但它不起作用;我想我的問題是數據類型:C程序在字符串中查找子字符串

int main() { 
    char text[58]; 
    strcpy(text, "I am James, James the Great, a pretty cool guy named James"); 
    char store[16]; 
    char name[6] = "James"; 
    for (int i = 0; i <= 16; i++) { 
     if (text[i] == 'J') { 
      for (int k = i; k < i + 6; k++) { 
       store[k] = text[i]; 
       } 
     } 
    } 
    int a = sizeof(store)/sizeof(store[0]); 
    for (int b = 0; b < a; b++) { 
     /* This should print out the values stored in the store array */ 
     printf("%d", store[b]); 
    } 
    return 0; 
} 

我預期的結果會是這樣的:

JamesJamesJames 

相反,我得到:

10000747474747474-6374747474 

所以我假設它在做什麼我告訴它這樣做,只是不把字母作爲字母存儲到數組中,有點令人困惑。有一個更好的方法嗎?

這是JavaScript代碼,我試圖翻譯:

var text = "I am James, James the Great, a pretty cool guy named James"; 

var myName = "James"; 
var hits =[]; 
for (i = 0; i < text.length; i++) { 
    if (text[i] === "J") { 
     for (var j = i; j < i + myName.length ; j++) { 
      hits.push(text[j]); 
     } 
    } 
} 
if (hits.length === 0) { 
    console.log("Your name wasn't found!"); 
} else{ 
    console.log(hits); 
} 
+1

0)' 「%d」' - >' 「%C」' – BLUEPIXY 2015-04-05 16:38:55

+0

是有一些問題,簡單地使用系統功能:)的strstr(? – user3629249 2015-04-05 20:44:54

回答

0

你犯了下列錯誤。

for (int i = 0; i <= 16; i++) 

您重複'16',但這是不正確的文本長度。

for (int k = i; k < i + 6; k++) 

所以你迭代的 「I,I + 1,I + 2,i + 3中,I + 4,I + 5,I + 6」 在這裏是7個字符, 「詹姆斯」 有6

store[k] = text[i]; 

你不改變「我」的價值,所以文字[我]總是返回'J'。最好使用savePoistion變量,所以在下面的代碼中。

int a = sizeof(store)/sizeof(store[0]); 
    for (int b = 0; b < a; b++) { 
     /* This should print out the values stored in the store array */ 
     printf("%d", store[b]); 
    } 

我不知道你在這裏做什麼。這真的沒有必要。

printf("%d", store[b]); 

「%d」表示您正在打印整數,因此不要驚訝您的輸出是數字。只需簡單地printf(「%s \ n」,store);

這看起來應該是這樣

int main() { 
    char text[58]; 
    strcpy(text, "I am James, James the Great, a pretty cool guy named James"); 
    char store[20]; 
    int len = strlen(text); 
    int savePosition = 0; 

    int i, k; 
    for (i = 0; i < len; i++) { 
     if (text[i] == 'J') { 
      for (k = i; k < i + 5; k++) { 
       store[savePosition] = text[k]; 
       ++savePosition; 
       } 
     } 
    } 
    store[savePosition] = '\0'; 
    printf("%s\n", store); 
    return 0; 
} 
+0

是的,我認爲我早些時候把它作爲%s,但它意外變成了%d。非常感謝你,很好的回答,並且很容易遵循! – steelcowboy 2015-04-05 17:45:28

0
int main (void) { 
    char text[] = "I am James, James the Great, a pretty cool guy named James"; 
    char name[] = "James"; 
    char store[16] = {0}; 
    int store_index = 0; 
    for (int i = 0; i < strlen(text); i++) { 
     if (text[i] == 'J') { 
      for (int k = i; k < i + strlen(name); k++) { 
       store[store_index++] = text[k]; 
      } 
     } 
    } 
    if(strlen(store)==0) { 
     printf("Your name wasn't found!\n"); 
    } else { 
     printf("%s\n", store); 
    } 
    return 0; 
} 

重寫版本。

int main (void) { 
    char text[] = "I am James, James the Great, a pretty cool guy named James"; 
    char name[] = "James"; 
    char store[sizeof(text)]; 
    char *p = strstr(text, name); 
    if(p == NULL) { 
     printf("Your name wasn't found!\n"); 
    } else { 
     int len = strlen(name); 
     int index = 0; 
     do { 
      memcpy(store + index, name, len); 
      index += len; 
      p = strstr(p + len, name); 
     } while(p != NULL); 
     store[index] = 0; 

     printf("%s\n", store); 
    } 
    return 0; 
} 
+1

只是打了一個更好的答案,但我建議'for(int i = 0; i <= strlen(text)-strlen(name); i ++)' – 2015-04-05 16:57:06

+0

@WeatherVane是的,這段代碼有問題。但我試圖翻譯JavaScript版本。 – BLUEPIXY 2015-04-05 16:59:11

+0

偉大的代碼,但有點難以關聯的JavaScript哈哈。但是,這絕對會幫助我更好地感受C,謝謝! – steelcowboy 2015-04-05 17:54:58

1

你要打破store[]這一行

for (int k = i; k < i + 6; k++) { 

,因爲你寫相同的索引您發現文中的text[]。您還一環太多次(6)

int ind = 0; 
for (int k = 0; k < 5; k++) { 
    store[ind++] = text[i+k]; 
} 

然後你的下一個語句不告訴你有關的任何數據收集你

int a = sizeof(store)/sizeof(store[0]); 

只是將其刪除,並從store[]打印ind字符。