1.strcpy
Copy stringchar * strcpy ( char * destination, const char * source );
To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.Parameters
destination
Return Value
destination is returned.
/*strcpy fucntion*/
#include<string.h>
char* (strcpy)(char*destination,char* source)
{
char* temp=destination;
for(temp=destination;(*temp++=*source++)!='/0';)
;
return(*destination);
}
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* strcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s/nstr2: %s/nstr3: %s/n",str1,str2,str3);
return 0;
}
Output:
str1: Sample string
str2: Sample string
str3: copy successful
|
2.strcat
char * strcat ( char * destination, const char * source );
Concatenate strings
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.
Parameters
- destination
- Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string. source
- C string to be appended. This should not overlap destination.
Return Value
destination is returned.
Strcat Fucntion
#include<string.h>
char* (strcat)(char* destination,char* source)
{
char* temp;
for(temp=destination;*source!='/0';)
;
for(;(*temp=*source)!='/0';++temp,++source)
;
return(destination);
}
3.strcmp
Compare two stringsint strcmp ( const char * str1, const char * str2 );
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminanting null-character is reached.
Parameters
str1
Return Value
Returns an integral value indicating the relationship between the strings:
A zero value indicates that both strings are equal.
A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
strcmp Function
#include<string.h>
char* (strcmp)(const char* str1,const char* str2)
{
for(;*str1=*str2;++str2,++str2)
if(*str1=='/0')
return(0);
return((*unsigned char*)str1<(*unsigned char*)str2)?-1:1);
}
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* strcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}
Output:
Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!
|
Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Output:
these strings are concatenated.
|

本文详细介绍了C语言中处理字符串的三个核心函数:strcpy用于复制字符串,strcat用于连接字符串,以及strcmp用于比较字符串。通过实例展示了这些函数的具体用法。

被折叠的 条评论
为什么被折叠?



