I need help copying an array char by char

I need help copying an array char by char for homework assignment.

1
2
3
4
5
6
7
/*****************************************************************************
* Function name:	void copyWord(char dest[], const char source[]);
* Description:		Copies source to destination.  (same as strcpy())
* Argument:			char dest[] (copy destination)
*					const char source[](array that is being copyed)
* Returns:			void
******************************************************************************/


1
2
3
4
5
6
7
8
void copyWord(char dest[], const char source[]){
	for(int ndx=0;ndx<MAX_WORD_LENGTH;ndx++){
		if (source[ndx]==NULL)
			return;
		else if (source[ndx]!=NULL)
			dest[ndx]=source[ndx];
	}
}



1
2
3
4
5
char test[]={"Test"};
char test2[]={""};
cout << "Testing copyWord()"<< endl;
copyWord(test2,test);
cout << test <<"==>" << test2 << endl;



The output is

Testing copyWord()
Test==>Test╠╠╠╠╠Test
Last edited on
Hey i figured the problem out my arrays weren't big enough

1
2
3
4
5
6
7
8
int const MAX_WORD_LENGTH=80;

void main (){

	char test[MAX_WORD_LENGTH]={"Test"};
	char test2[MAX_WORD_LENGTH]={""};
	char subWordTestSource[MAX_WORD_LENGTH]={"0123456789"};
	char subWordTestDestination[MAX_WORD_LENGTH]={""};
This is in C-C++ std method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

char* copyWord(char* w1,char* w2){
     int i,j;
     for(i=0;i<strlen(w1);i++){
     w2[i]=w1[i];
     if(i!=strlen(w1)-1)
     {}
     else
     w2[strlen(w1)]=' ';
     }
     return w2;
     }

int main()
{
  char word1[]={"Test"};
  char word2[]={""};
  if(copyWord(word1,word2))
  printf("%s%s","Testing copyWord()","\n");
  printf("%s%s",word2,"\n");
  system("PAUSE");	
  return 0;
}
Topic archived. No new replies allowed.