1.strcpy
char * strcpy ( char * destination, const char * source );
Copy string
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
- Pointer to the destination array where the content is to be copied. source
- C string to be copied.
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 | |
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
int strcmp ( const char * str1, const char * str2 );
Compare two strings
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
- C string to be compared. str2
- C string to be compared.
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 | |
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.
|