2012-10-02 56 views
0

我想獲得一個函數來分割一個字符串,該字符串包含幾個由1或多個空格分隔的單詞,並將每個單詞沒有任何空格放入一個索引字符串數組。將一個字符串中的字放入一個字符串數組中 - C編程

我一直在Google上搜索它,似乎我需要strtok,但我有點無知,有人請擺脫一些光?

+1

的http://stackoverflow.com/questions/12582955/remove-whitespaces-tail-from-string-char –

+0

不是真的,我不希望刪除空格可能重複,我更關心的是獲得單詞 – PhillToronto

回答

4
/* strtok example */ 
#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    char str[] ="- This, a sample string."; 
    char * pch; 
    printf ("Splitting string \"%s\" into tokens:\n",str); 
    pch = strtok (str," "); 
    while (pch != NULL) 
    { 
    printf ("%s\n",pch); 
    pch = strtok (NULL, " "); 
    } 
    return 0; 
} 
+0

謝謝@ andro1d這個好開始。我能告訴strtok忽略\ n和\ t嗎?例: 例如: - 這是一個樣本\ t字符串。以產生結果: - This, a sample 字符串 – PhillToronto

+0

因爲它現在是它\ t單獨放在一行。謝謝 :) – PhillToronto

相關問題