C程序 - 如何从字符串中读取角色

  • 本文关键字:字符串 读取 角色 程序 c
  • 更新时间 :
  • 英文 :

#include<stdio.h>  
void main()   
{
char source[30]="salai word2 word3";   
char destination[20][20];   
printf(" nsource is: %snn", source);   
getch();  
}

在上述程序中,char变量"源"包含三个单词(由空间隔开)。我如何读取"源"中的单词并存储在另一个字符阵列"目的地"中。期望如下:

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

您可以使用循环。插入字符,直到获得空间为止。您可以尝试一下。

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];
   }

最新更新