2016-12-21 28 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"には3つの単語(スペースで区切られています)が含まれています。どのようにして "ソース"から単語を読み込んで、別のchar配列 "宛先"に格納できますか?期待値は次のとおりです。Cプログラム - 文字列から文字を部分的に読み取る方法

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

と 'strtok()'、おそらく –

+0

この「how」は広すぎる、VTCです。また、あなたの努力を時間の経過とともに見せてください。 –

+0

こちらをご覧ください http://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c – Hast

答えて

0

ループを使用できます。空白ができるまで文字を挿入します。これを試すことができます。

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]; 
    } 
関連する問題