Hi, guys trying to implement own strcpy function, but no output is being printed. Please help - #include<stdio.h>
-
#include<string.h>
-
-
void strcpy_own(char dest[], char src[], int len);
-
-
int main()
-
{
-
char src[15]="jeevan", dest[15];
-
int length=0;
-
length = strlen(src)+1;
-
strcpy_own(dest,src,length);
-
return 0;
-
}
-
-
void strcpy_own(char dest[], char src[], int len)
-
{
-
int i=0;
-
for(i=0;i<len;i++)
-
{
-
while(src[i]!='\0')
-
dest[i]=src[i];
-
-
}
-
dest[strlen(dest)+1]='\0';
-
printf("%s",dest);
-
getchar();
-
}
if you are implementing a strcpy() you don't need the length parameter, try this - int i=0;
-
for(i=0; src[i]!=0; i++)
-
dest[i]=src[i];
-
dest[i]=0;
-
printf("out %s",dest);
-
or even - int i=0;
-
do
-
dest[i]=src[i];
-
while(dest[i++]!=0);
-
printf("out %s",dest);
-
if the source does not contain a 0 you end up corrupting memory
if implementing strncpy() you need the length parameter
3 1284
lines 20 and 21 - while(src[i]!='\0')
-
dest[i]=src[i];
-
-
if src[i] is not a '\0' will go into an endless loop
remove line 20
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?
if you are implementing a strcpy() you don't need the length parameter, try this - int i=0;
-
for(i=0; src[i]!=0; i++)
-
dest[i]=src[i];
-
dest[i]=0;
-
printf("out %s",dest);
-
or even - int i=0;
-
do
-
dest[i]=src[i];
-
while(dest[i++]!=0);
-
printf("out %s",dest);
-
if the source does not contain a 0 you end up corrupting memory
if implementing strncpy() you need the length parameter
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
...
|
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...
|
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;
}
|
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...
|
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...
|
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...
|
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...
|
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
|
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>...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |