C中string中一些基本函數的介紹與實現

1.strcpy

char * strcpy ( char * destination, const char * source );

Copy string

Copies the C string pointed by source into the array pointed by destination, including the terminating null character.

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
/* 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

int strcmp ( const char * str1, const char * str2 );

Compare two strings

Compares the C string str1 to the C string 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
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
/* 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
/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}



Output:


these strings are concatenated. 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章