2016-10-20 26 views
0

這裏我寫了一段代碼,它不使用strtok()函數就可以將單詞從給定的句子中分離出來,但是問題是它只打印第一個單詞。爲什麼其餘的單詞不用'不打印?把單詞從一個沒有庫函數的句子中分離出來

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

int main() 
{ 
    char str[] ="This, a sample string."; 
    int i=0,k,begin,end; 
    while(str[i]!='\0'){ 
    while(str[i]==' '){ 
     i++; 
    } 
    begin = i; 
    while(str[i]!=' ' && str[i]!='\0'){ 
     i++; 
    } 
    end = i-1; 
    char *ptr =(char *)malloc((end-begin)*sizeof(char)); 
    for(k=begin;k<=end;k++){ 
     ptr[k] = str[k]; 
    } 

    printf("%s\n",ptr); 
    if(str[i]=='\0'){ 
     break; 
    } 
    } 
} 
+0

這不會打印*任何東西*更不用說第一個字。沒有'puts','printf','putc',基本上在發佈代碼*中根本沒有控制檯輸出*。 – WhozCraig

+1

不分配足夠的內存,不寫入字符串終止符。 –

回答

0

如果你允許修改就地串,你可以不喜歡

#include <stdio.h> 

static void print_words(char *str) { 
    char *start = str; // Word start pointer. 
    for(;*str;str++) { // While there are characters... 
    if(*str == ' ') { // Space found! 
     *str = '\0'; // Replace the space with a null to end the string 
     printf("%s\n", start); // Output the now null-delimited string 
     *str = ' '; // Return the string to its original form 
     start = str + 1; // Advance the word-start pointer 
    } 
    } 
    printf("%s\n", start); // Print the last word remaining unprinted, if any 
} 

int main() 
{ 
    char str[] = "This, a sample string."; 
    print_words(str); 
} 
+0

如果兩個單詞之間有多個空格,這將不起作用! –

0

你已經錯過了在你的代碼中的一些邏輯。

  1. 默認情況下,您正在遍歷第二個單詞。這不是你的要求。

  2. 該塊識別給定字符串中的單詞。

  3. 您的分配比我們需要的少一個字節。 終止字符串需要一個額外的字節。空字符。

** 4。 ptr [k] = str [k] 這是代碼中的漏洞。

這應該是這樣的ptr [0] = STR [K]; **

  • 你錯過遞增i值以進一步迭代。
  • ==================要點1 =======================

    while(str[i]!='\0'){ 
        while(str[i]==' '){  -> this is point no 1. This logic will skip first word 
         i++; 
        } 
    
    =================point 2 ================ 
    begin = i; 
        while(str[i]!=' ' && str[i]!='\0'){ 
         i++; 
        } 
        **end = i-1;** 
    ========================================== 
    
        char *ptr =(char *)malloc((end-begin)*sizeof(char)); -> point 3. 
    
    point 4 : 
    
        for(k=begin;k<=end;k++){ 
         ptr[k] = str[k]; 
        } 
    
    
        printf("%s\n",ptr); 
        if(str[i]=='\0'){ 
         break; 
        } 
    point 5 : 
        else 
         i++; 
        } 
    

    -Thanks

    1

    完整的代碼應該是這樣的。

    我已更改代碼上的代碼。

    int main() 
    { 
         char str[] ="This, a sample string."; 
         int i=0,k,begin,end; 
    
         int t; 
    
         while(str[i]!='\0'){ 
           begin = i; 
           while(str[i]!=' ' && str[i]!='\0'){ 
             i++; 
           } 
           end = i-1; 
    
           char *ptr = (char *) malloc((i-begin)*sizeof(char) + 1); 
    
           t=0; 
           for(k=begin;k<=end;k++){ 
             ptr[t++] = str[k]; 
           } 
           ptr[t] = '\0'; 
    
           puts(ptr); 
    
           if(str[i]=='\0'){ 
             break; 
           } else 
             while(str[i] == ' ') i++; //To avoid multiple spaces 
         } 
    
         return 0; 
    } 
    

    -Thanks

    1

    與您的代碼的問題是在這裏:

    for(k=begin;k<=end;k++){ 
         ptr[k] = str[k]; 
        } 
    

    k被用作兩個ptrstr索引變量,你應該做的是使用一個單獨的變量爲ptr,以便填充來自0索引的字符。

    此外,您沒有分配正確數量的空間。字符數爲end - begin + 1,您還必須爲null character字符串終止字符)分配空間。

    所以,正確的分配是:

    char *ptr =(char *)malloc((end-begin+2)*sizeof(char)); 
    

    和你正確的代碼是:

    int index = 0; 
    for(k = begin;k <= end; k++){ 
        ptr[jj++] = str[k]; 
    } 
    ptr[end - begin + 1] = '\0'; 
    
    +0

    非常感謝:) –

    +0

    @ AL-zami你可以接受,如果我的回答幫助你 –

    0

    我已經發現了這個問題,並提出瞭解決方案:

    1。第一個問題是我不分配正確的值*ptr。所以,我用一個變量j從當前索引

    2.I爲null終止每個捕獲

    ptr[k]='\0'; 
    

    3所需的起點開始。所以我總所做的更改如下:

    for(k=0,j=begin;k<=end-begin;k++,j++){ // use a variable j to start from the beginning of current index 
        ptr[k] = str[j]; 
    } 
    ptr[k]='\0'; 
    

    全碼:

    #include <stdio.h> 
    #include <string.h> 
    
    int main() 
    { 
        char str[] ="This, an sample string."; 
        int i=0,j,k,begin,end; 
        char *ptr; 
        while(str[i]!='\0'){ 
        while(str[i]==' '){ 
         i++; 
        } 
        begin = i; 
        while(str[i]!=' ' && str[i]!='\0'){ 
         i++; 
        } 
        end = i-1; 
        ptr =(char *)malloc((end-begin)*sizeof(char)); 
    
        for(k=0,j=begin;k<=end-begin;k++,j++){ // use a variable j to start from the beginning of current index 
         ptr[k] = str[j]; 
        } 
        ptr[k]='\0'; 
    
        printf("%s\n",ptr); 
        if(str[i]=='\0'){ 
         break; 
        } 
        } 
    } 
    
    +1

    不要忘了'免費(ptr);' – sameerkn

    +0

    tnanks:)........ –

    相關問題