2016-12-21 102 views
-2
#include<stdio.h> 

void main() 
{ 

char source[30]="salai word2 word3"; 
char destination[20][20]; 

printf(" \n\source is: %s\n\n", source); 
getch(); 
} 

在上述程序中,char變量「source」包含三個單詞(用空格分隔)。我如何從「源」中讀取單詞並將其存儲在另一個字符數組「目標」中。期望如下:C程序 - 如何從字符串中逐個字符地讀取字符

strcpy(destination[0], salai) 
strcpy(destination[1], word2) 
strcpy(destination[2], word3) 
+0

和'strtok()',也許 –

+0

這個「怎麼樣」太寬了,VTC。此外,顯示你的努力,直到時間。, –

+0

看到這一個 http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c – Hast

回答

0

您可以使用循環。插入字符,直到您獲得空間。你可以試試這個。 VTC:

int i,j=0; 
int len = strlen(source); 
int k=0; 
for(int i=0;i<len;i++){ 
    if(source[i] == ' ') 
     { 
      k++; 
      j=0; 
     } 
    else 
     destination[k][j++]=source[i]; 
    }