473,379 Members | 1,377 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,379 software developers and data experts.

String Manipulation Functions - strcpy, strncpy

1. Can someone tell me the difference between these two functions:

void strcpy(char *s1, const char *s2)
{
while(*s1++ = *s2++)
;
}

//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function

Both functions are doing the same job.
Why the later function has to return a char pointer?

We call both functions in the same manner and we don't sssign the
return pointer to any other pointer. Correct me if I'm wrong.

-------------------------
2. This question is regarding strncpy library function.

//function prototype of strncpy follows
char *strncpy(char *s1, const char *s2, size_t n);
We need to pad '\0' to the end of s1 when n is less than the length of
s2 plus one.

Compare strncpy with strncat. The function strncat pad '\0' by itself.
Why this was left to the programmer in the case of strncpy function.

Apr 1 '07 #1
4 9515
chikito.chikito wrote:
1. Can someone tell me the difference between these two functions:
Firstly, you have a primarily C question, and this newsgroup works best for
C++.

C++ inherits the C Standard Library, including these functions, so everyone
here should know them. You need to learn C++ too, and only use high-level
systems like std::string in your programs. And if you have a reason to learn
and use C, you should send future questions like these to news:comp.lang.c .

Next, these questions are worded like homework. I will answer one in
grueling detail that might still get you in trouble, and snip the second
one. Read your tutorial - it's almost as erudite as me.
void strcpy(char *s1, const char *s2)
In general, never rename a library function and give it a different return
value or different contents. Your platform might rely on some detail of the
original function, and your new function might replace it. If you really
need an alternate string copy (doubtful), then you should give it a
different name, so nobody gets confused.

(And the C Standard reserves all identifiers beginning with str[a-z], so
follow that rule too.)
//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function

Both functions are doing the same job.
Why the later function has to return a char pointer?
As a convenience to chain calls, such as:

strcat(strcpy(slug, label), ": ");

And note the equivalent in C++ is much clearer:

slug = label + ": ";

--
Phlip
http://flea.sourceforge.net/PiglegToo_1.html
Apr 1 '07 #2
On Apr 1, 10:33 pm, "Phlip" <phlip...@yahoo.comwrote:
chikito.chikito wrote:
1. Can someone tell me the difference between these two functions:

Firstly, you have a primarily C question, and this newsgroup works best for
C++.

C++ inherits the C Standard Library, including these functions, so everyone
here should know them. You need to learn C++ too, and only use high-level
systems like std::string in your programs. And if you have a reason to learn
and use C, you should send future questions like these to news:comp.lang.c .

Next, these questions are worded like homework. I will answer one in
grueling detail that might still get you in trouble, and snip the second
one. Read your tutorial - it's almost as erudite as me.
void strcpy(char *s1, const char *s2)

In general, never rename a library function and give it a different return
value or different contents. Your platform might rely on some detail of the
original function, and your new function might replace it. If you really
need an alternate string copy (doubtful), then you should give it a
different name, so nobody gets confused.

(And the C Standard reserves all identifiers beginning with str[a-z], so
follow that rule too.)
//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function
Both functions are doing the same job.
Why the later function has to return a char pointer?

As a convenience to chain calls, such as:

strcat(strcpy(slug, label), ": ");

And note the equivalent in C++ is much clearer:

slug = label + ": ";

--
Phlip
http://flea.sourceforge.net/PiglegToo_1.html

Philip,

I understand the reasons now. Thanks.

Apr 1 '07 #3

<ch*************@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
1. Can someone tell me the difference between these two functions:

void strcpy(char *s1, const char *s2)
{
while(*s1++ = *s2++)
;
}

//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function

Both functions are doing the same job.
Why the later function has to return a char pointer?

We call both functions in the same manner and we don't sssign the
return pointer to any other pointer. Correct me if I'm wrong.
You can use the returned pointer if you need to. Consider something like:

printf( "%s", strcat( str1, str2 ) );
or other fuctions that accept a string.
-------------------------
2. This question is regarding strncpy library function.

//function prototype of strncpy follows
char *strncpy(char *s1, const char *s2, size_t n);
We need to pad '\0' to the end of s1 when n is less than the length of
s2 plus one.

Compare strncpy with strncat. The function strncat pad '\0' by itself.
Why this was left to the programmer in the case of strncpy function.
strncpy basically does the exact same thing memcpy does. It copies n bytes
starting at sr2 into sr1. Those bytes can be binary, which can include a
null terminator (\0). Also, you may want to copy into the middle of a
string and appending an \0 would mess it up.

char Text[100];
strcpy( Text, "The blue pen is here." );
char Color[100];
strcpy( Color, "red " );
strncpy( Text + 3, Color );

This would replace "blue" with "red "
If strncpy added a null terminator, if you printed it would output:
"The red "
because of the null terminator.

With strcat, however, you are adding a string to the end of another string,
so there will be text at the end of the first string anyway.
Apr 1 '07 #4
On Apr 1, 10:59 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
<chikito.chik...@gmail.comwrote in message

news:11*********************@b75g2000hsg.googlegro ups.com...
1. Can someone tell me the difference between these two functions:
void strcpy(char *s1, const char *s2)
{
while(*s1++ = *s2++)
;
}
//function prototype of strcpy follows
char *strcpy(char *s1, const char *s2) // library function
Both functions are doing the same job.
Why the later function has to return a char pointer?
We call both functions in the same manner and we don't sssign the
return pointer to any other pointer. Correct me if I'm wrong.

You can use the returned pointer if you need to. Consider something like:

printf( "%s", strcat( str1, str2 ) );
or other fuctions that accept a string.
-------------------------
2. This question is regarding strncpy library function.
//function prototype of strncpy follows
char *strncpy(char *s1, const char *s2, size_t n);
We need to pad '\0' to the end of s1 when n is less than the length of
s2 plus one.
Compare strncpy with strncat. The function strncat pad '\0' by itself.
Why this was left to the programmer in the case of strncpy function.

strncpy basically does the exact same thing memcpy does. It copies n bytes
starting at sr2 into sr1. Those bytes can be binary, which can include a
null terminator (\0). Also, you may want to copy into the middle of a
string and appending an \0 would mess it up.

char Text[100];
strcpy( Text, "The blue pen is here." );
char Color[100];
strcpy( Color, "red " );
strncpy( Text + 3, Color );

This would replace "blue" with "red "
If strncpy added a null terminator, if you printed it would output:
"The red "
because of the null terminator.

With strcat, however, you are adding a string to the end of another string,
so there will be text at the end of the first string anyway.

..... we may want to copy into the middle of a
string and appending an \0 would mess it up. So, it is better that the
programmer appends the \0 wherever necessary.

Thanks Langston

Apr 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjakowlew | last post by:
Hi, I'm trying to use some string manipulation from a file's path. filepath='c:\documents\web\zope\file.ext' I need to extract everthing after the last '\' and save it. I've looked around...
0
by: Sam Hart | last post by:
Hi, I need some string manipulation functions in C#, please help. I need to translate ServicesArray into PIN_FLD_SERVICES and FldServiceEmail into PIN_FLD_SERVICE_EMAIL (into upper case),...
7
by: John A Grandy | last post by:
what are the preferred VB.NET analogues for IsNumeric() and Len() and CInt() & similar string-manipulation functions in VB6
11
by: John A Grandy | last post by:
what is wrong with this ? (assuming s is > 1 char long) Dim s As String s = s.ToUpper(s.Substring(1, 1)) & s.ToLower(s.Substring(2))
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
3
by: douglas wittner | last post by:
morning all, can someone help me find the most efficient way of manipulating a large file. i need to replace special characters in a large file and multiple string.replace functions are causing...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
1
by: adam bob | last post by:
Hello, I'm struggling with an image mechanism I'm trying to build which basically manipulates a URL string. This is the sort URL that is gained from an InfoPath form ...
2
by: disent | last post by:
Hello, I should tell you I don't know much about C++ before i get started. Ok, what I need is a way to replace some letters between the marks i choose. For example: string str1 =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.