-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();  
}

In the above program, char variable "source" contains three words(separated by space). How can I read the words from "source" and store in another char array "destination". Expectation as follows:

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

1 Answers1

1

You can use loop. Insert the characters until you get a space. You can try this.

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