answersLogoWhite

0


Best Answer

There is almost nothing to explain if you know C Language. Here is the program:

#include

void copyString(const char *src, char *dest);

int main() {

char str1[100];

char str2[100];

printf("Enter the string: ");

gets(str1);

copyString(str1, str2);

printf("Copied string: %s\n", str2);

return 0;

}

void copyString(const char *src, char *dest) {

while (*dest++ = *src++);

}

As you can see actually copying is done in just one line of code. While loop stops after it reaches zero value and all strings in C language are null-terminated strings (ending with 0x00 byte at the end of string, which is zero).

Testing:

Enter the string: Hello world, we have copy function for string!

Copied string: Hello world, we have copy function for string! Note: You should not be using gets() in real application, because it is not possible to limit number of the characters to be read thus allowing to overflow buffer. You might get a warning in line with our while loop if you are compiling with -Wall option in GCC, what does -Wall is that it checks for questionable places. This place for compiler is questionable because we have assignment operation inside while loop expression. Most of the times this is common mistake in programming, but not in this situation. Compiler just give a notice for developer that we should be careful.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

A string in C is a character array, so you could simply copy one character at a time using a loop. (Be careful, things like this are the cause of many security holes using buffer overflows)

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

void myhomework (char *to, const char *from)

{ while (*to++ = *from++); }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you copy a string without using string functions?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you get odd ascii letters from a given string using string copy?

there is no such method using string copy


Write a program to concate the string without using strconcat function?

If you don't need to preserve the first string you could just iterate over the second string and copy each character onto the end of the first string, then return that


Write a c program to copy the string without using strcpy?

use strcat, strncpy, stpcpy, sprintf, strlen+memcpy, etc


How do you reverse a given string without using string functions?

1.take while loop in which u continue up to null in array of string. 2.once u get null pointer then take a new array of same length. 3.when u get null there ll save position no in variable i. 4.take while loop i to 0; 5.and j=0 for new array. 6.in above loop copy old array to new array in each cycle of loop. 7.j++; 8.u ll get reverse of string.


Can you have an ipod without using iTunes?

you can have an iPod without an iTunes, using Copy trans suite.it's a free program.


C program to copy two strings in to a new string with out using stringcopy in borland c?

You can use so called concatenation of strings:{...string str1 = "something here";string str2 = " and something here";string newStr = str1 + str2;...}


Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in VB?

what is string


How do you write C plus plus string concat without builtin functions?

#include<iostream> #include<string> int main() { // the two strings to concatenate std::string str1 = "Hello "; std::string str2 = "world!"; // allocate memory to the concatenated string with null-terminator char* str3 = new char[str1.size() + str2.size() + 1]; // initialise a moving pointer char* p = str3; // copy from the first string memcpy( p, str1.c_str(), str1.size() ); // advance the pointer p += str1.size(); // copy from the second string memcpy( p, str2.c_str(), str2.size() ); // advance the pointer p += str2.size(); // set the null-terminator *p = 0; // print concatenated string std::cout << str3 << std::endl; // tidy up delete [] str3, str3 = NULL; }


Functions of crt in a computer?

Output visual data - they allow us to avoid using hard copy only (printers) as output.


How do you copy one string to another without using function in c plus plus?

If you're using the std::string, why don't you just create another string instance like this: std::string hello = "hello"; std::string hello1; hello1 = hello; This will have copied the contents of the variable hello into hello1; char *psz1 = "This is a test."; char *psz2 = "..............."; // note - must be same or greater number of characters as psz1 char *p1 = psz1; char *p2 = psz2; while ((*p2++ = *p1++) != '\0'); // copy p1 to p2 Note: This is a trivial example, and might not work on most modern compilers that treat string literals as const or that place them into read-only memory. To solve this you need to replace the second line with ... char *psz2 = new char[16]; ... and then test to make sure that psz1 != NULL.


Can you copy a backup copy of an Xbox 360 game?

The only way to copy Xbox 360 games and play them on your 360 without using a modchip is to use the guide at: http://copyxbox360games.blogspot.com


Can you write a c program to copy one string to another without using library function?

#include<stdio.h> void main() { int str[20],str1[20],i,j=1; printf("enter the string"); gets(str); for(i=1;str[i]!='\0';i++) { str1[j]=str[i]; j++; } if(str1[j]!='\0') { puts(str1); } getch(); }