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. 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值