473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

No output for own implementation of strcpy in c

Hi, guys trying to implement own strcpy function, but no output is being printed. Please help

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. void strcpy_own(char dest[], char src[], int len);
  5.  
  6. int main()
  7. {
  8.     char src[15]="jeevan", dest[15];
  9.     int length=0;
  10.     length = strlen(src)+1;
  11.     strcpy_own(dest,src,length);
  12.     return 0;
  13. }
  14.  
  15. void strcpy_own(char dest[], char src[], int len)
  16. {
  17.     int i=0;
  18.     for(i=0;i<len;i++)
  19.     {
  20.         while(src[i]!='\0')
  21.         dest[i]=src[i];
  22.  
  23.     }
  24.     dest[strlen(dest)+1]='\0';
  25.             printf("%s",dest);
  26.             getchar();
  27. }
Aug 20 '14 #1

✓ answered by horace1

if you are implementing a strcpy() you don't need the length parameter, try this
Expand|Select|Wrap|Line Numbers
  1.     int i=0;
  2.     for(i=0; src[i]!=0; i++)
  3.          dest[i]=src[i];
  4.     dest[i]=0;
  5.     printf("out %s",dest);
  6.  
or even
Expand|Select|Wrap|Line Numbers
  1.     int i=0;
  2.     do
  3.          dest[i]=src[i];
  4.     while(dest[i++]!=0);
  5.     printf("out %s",dest);
  6.  
if the source does not contain a 0 you end up corrupting memory
if implementing strncpy() you need the length parameter

3 1284
horace1
1,510 Expert 1GB
lines 20 and 21
Expand|Select|Wrap|Line Numbers
  1.     while(src[i]!='\0')
  2.          dest[i]=src[i];
  3.  
  4.  
if src[i] is not a '\0' will go into an endless loop
remove line 20
Aug 20 '14 #2
Hey thanks man but can i replace while with if? I replaced and getting output but i get a weird character at the end after jeevan like jeevan? .. Am I doing any other mistake here?
Aug 20 '14 #3
horace1
1,510 Expert 1GB
if you are implementing a strcpy() you don't need the length parameter, try this
Expand|Select|Wrap|Line Numbers
  1.     int i=0;
  2.     for(i=0; src[i]!=0; i++)
  3.          dest[i]=src[i];
  4.     dest[i]=0;
  5.     printf("out %s",dest);
  6.  
or even
Expand|Select|Wrap|Line Numbers
  1.     int i=0;
  2.     do
  3.          dest[i]=src[i];
  4.     while(dest[i++]!=0);
  5.     printf("out %s",dest);
  6.  
if the source does not contain a 0 you end up corrupting memory
if implementing strncpy() you need the length parameter
Aug 20 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Johan Lindh | last post by:
Where can I find a comprehensive test suite for printf()? It seems like most implementations go wrong at sensitive spots, like printf("#.0o",0) (which should print "0") printing nothing at all. ...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
5
by: Kuku | last post by:
Would the following program give the same output at all times int main(int argc, char *argv) { strcpy(argv, "Hello"); strcpy(argv,"Good Morning"); printf("%s\n%s\n",argv,argv); return 0; }
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
27
by: Simon Biber | last post by:
The following Example 3 is given in the 1999 C standard for the function fscanf: I have tested several implementations and none of them get the last case right. In no case does fscanf return 0...
12
by: vj | last post by:
I am using Borland C++. I run a program which generates output data files, say 'Voltage.dat', 'Current.dat'. I have a variable in my code (say N), and for various values of N (for each value of...
28
by: Mahesh | last post by:
Hi, I am looking for efficient string cancatination c code. I did it using ptr but my code i think is not efficient. Please help. Thanks a lot
77
by: arnuld | last post by:
I have created my own implementation of strcpy library function. I would like to have comments for improvements: /* My version of "strcpy - a C Library Function */ #include <stdio.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.