Answer the question
In order to leave comments, you need to log in
What do while(*from) and *to = " mean? Up to what point will the loop run?
The program was written using arrays, strings, and pointers.
The general format of the strcpy() function is:
strcpy(to, from);
The strcpy() function copies the contents of the from string to the to string.
Source:
#include <iostream>
#include <cstdio>
using namespace std;
void mystrcpy(char *to, char *from);
int main()
{
char str1[20], str2[20];
cout << «enter string: «;
gets(str1);
mystrcpy(str2, str1);
cout << «string copiet: » << str2;
cin.get();
return 0;
}
void mystrcpy(char *to, char *from)
{
while(*from)
{
*to = *from;
to++;
from++;
}
*to = »;
}
Answer the question
In order to leave comments, you need to log in
*from - getting the value at a specific address pointed to by the pointer.
0 = false for the while(...)
from++ condition - move to the next value. The important thing to remember here is that (address = address + sizeof(T)), where T is the value type, not a reference to it.
The classic notation for this function is
void mystrcpy(char *to, char *from)
{
while((*to++ = *from++))
;
}
#include <iostream>
#include <cstdio>
using namespace std;
void mystrcpy(char *to, char *from);
int main()
{
char str1[20], str2[20];
cout << "enter string: ";
gets(str1);
mystrcpy(str2, str1);
cout << "string copiet: " << str2;
cin.get();
return 0;
}
void mystrcpy(char *to, char *from)
{
while((*to++ = *from++))
;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question