C标准库学习string.h

<string.h>

typedef unsigned int size_t;
typedef unsigned short wchar_t;

#define _NLSCMPERROR    2147483647  /* currently == INT_MAX */

/* Define NULL pointer value */

#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif


/* Function prototypes */

#ifdef  _M_MRX000
_CRTIMP void *  __cdecl memcpy(void *, const void *, size_t);
_CRTIMP int     __cdecl memcmp(const void *, const void *, size_t);
_CRTIMP void *  __cdecl memset(void *, int, size_t);
_CRTIMP char *  __cdecl _strset(char *, int);
_CRTIMP char *  __cdecl strcpy(char *, const char *);
_CRTIMP char *  __cdecl strcat(char *, const char *);
_CRTIMP int     __cdecl strcmp(const char *, const char *);
_CRTIMP size_t  __cdecl strlen(const char *);
#else
        void *  __cdecl memcpy(void *, const void *, size_t);
        int     __cdecl memcmp(const void *, const void *, size_t);
        void *  __cdecl memset(void *, int, size_t);
        char *  __cdecl _strset(char *, int);
        char *  __cdecl strcpy(char *, const char *);
        char *  __cdecl strcat(char *, const char *);
        int     __cdecl strcmp(const char *, const char *);
        size_t  __cdecl strlen(const char *);
#endif

_CRTIMP void *  __cdecl _memccpy(void *, const void *, int, unsigned int);
_CRTIMP void *  __cdecl memchr(const void *, int, size_t);
_CRTIMP int     __cdecl _memicmp(const void *, const void *, unsigned int);

#ifdef  _M_ALPHA
        /* memmove is available as an intrinsic in the Alpha compiler */
        void *  __cdecl memmove(void *, const void *, size_t);
#else
_CRTIMP void *  __cdecl memmove(void *, const void *, size_t);
#endif


_CRTIMP char *  __cdecl strchr(const char *, int);
_CRTIMP int     __cdecl _strcmpi(const char *, const char *);
_CRTIMP int     __cdecl _stricmp(const char *, const char *);
_CRTIMP int     __cdecl strcoll(const char *, const char *);
_CRTIMP int     __cdecl _stricoll(const char *, const char *);
_CRTIMP int     __cdecl _strncoll(const char *, const char *, size_t);
_CRTIMP int     __cdecl _strnicoll(const char *, const char *, size_t);
_CRTIMP size_t  __cdecl strcspn(const char *, const char *);
_CRTIMP char *  __cdecl _strdup(const char *);
_CRTIMP char *  __cdecl _strerror(const char *);
_CRTIMP char *  __cdecl strerror(int);
_CRTIMP char *  __cdecl _strlwr(char *);
_CRTIMP char *  __cdecl strncat(char *, const char *, size_t);
_CRTIMP int     __cdecl strncmp(const char *, const char *, size_t);
_CRTIMP int     __cdecl _strnicmp(const char *, const char *, size_t);
_CRTIMP char *  __cdecl strncpy(char *, const char *, size_t);
_CRTIMP char *  __cdecl _strnset(char *, int, size_t);
_CRTIMP char *  __cdecl strpbrk(const char *, const char *);
_CRTIMP char *  __cdecl strrchr(const char *, int);
_CRTIMP char *  __cdecl _strrev(char *);
_CRTIMP size_t  __cdecl strspn(const char *, const char *);
_CRTIMP char *  __cdecl strstr(const char *, const char *);
_CRTIMP char *  __cdecl strtok(char *, const char *);
_CRTIMP char *  __cdecl _strupr(char *);
_CRTIMP size_t  __cdecl strxfrm (char *, const char *, size_t);

#ifdef  _MAC
unsigned char * __cdecl _c2pstr(char *);
char * __cdecl _p2cstr(unsigned char *);

#if     !__STDC__
__inline unsigned char * __cdecl c2pstr(char *sz) { return _c2pstr(sz);};
__inline char * __cdecl p2cstr(unsigned char *sz) { return _p2cstr(sz);};
#endif
#endif

#if     !__STDC__

/* prototypes for oldnames.lib functions */
_CRTIMP void * __cdecl memccpy(void *, const void *, int, unsigned int);
_CRTIMP int __cdecl memicmp(const void *, const void *, unsigned int);
_CRTIMP int __cdecl strcmpi(const char *, const char *);
_CRTIMP int __cdecl stricmp(const char *, const char *);
_CRTIMP char * __cdecl strdup(const char *);
_CRTIMP char * __cdecl strlwr(char *);
_CRTIMP int __cdecl strnicmp(const char *, const char *, size_t);
_CRTIMP char * __cdecl strnset(char *, int, size_t);
_CRTIMP char * __cdecl strrev(char *);
        char * __cdecl strset(char *, int);
_CRTIMP char * __cdecl strupr(char *);

#endif  /* !__STDC__ */


#ifndef _MAC
#ifndef _WSTRING_DEFINED

/* wide function prototypes, also declared in wchar.h  */

_CRTIMP wchar_t * __cdecl wcscat(wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcschr(const wchar_t *, wchar_t);
_CRTIMP int __cdecl wcscmp(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcscpy(wchar_t *, const wchar_t *);
_CRTIMP size_t __cdecl wcscspn(const wchar_t *, const wchar_t *);
_CRTIMP size_t __cdecl wcslen(const wchar_t *);
_CRTIMP wchar_t * __cdecl wcsncat(wchar_t *, const wchar_t *, size_t);
_CRTIMP int __cdecl wcsncmp(const wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl wcsncpy(wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl wcspbrk(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcsrchr(const wchar_t *, wchar_t);
_CRTIMP size_t __cdecl wcsspn(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcsstr(const wchar_t *, const wchar_t *);
_CRTIMP wchar_t * __cdecl wcstok(wchar_t *, const wchar_t *);

_CRTIMP wchar_t * __cdecl _wcsdup(const wchar_t *);
_CRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *);
_CRTIMP int __cdecl _wcsnicmp(const wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl _wcsnset(wchar_t *, wchar_t, size_t);
_CRTIMP wchar_t * __cdecl _wcsrev(wchar_t *);
_CRTIMP wchar_t * __cdecl _wcsset(wchar_t *, wchar_t);

_CRTIMP wchar_t * __cdecl _wcslwr(wchar_t *);
_CRTIMP wchar_t * __cdecl _wcsupr(wchar_t *);
_CRTIMP size_t __cdecl wcsxfrm(wchar_t *, const wchar_t *, size_t);
_CRTIMP int __cdecl wcscoll(const wchar_t *, const wchar_t *);
_CRTIMP int __cdecl _wcsicoll(const wchar_t *, const wchar_t *);
_CRTIMP int __cdecl _wcsncoll(const wchar_t *, const wchar_t *, size_t);
_CRTIMP int __cdecl _wcsnicoll(const wchar_t *, const wchar_t *, size_t);

#if     !__STDC__

/* old names */
#define wcswcs wcsstr

/* prototypes for oldnames.lib functions */
_CRTIMP wchar_t * __cdecl wcsdup(const wchar_t *);
_CRTIMP int __cdecl wcsicmp(const wchar_t *, const wchar_t *);
_CRTIMP int __cdecl wcsnicmp(const wchar_t *, const wchar_t *, size_t);
_CRTIMP wchar_t * __cdecl wcsnset(wchar_t *, wchar_t, size_t);
_CRTIMP wchar_t * __cdecl wcsrev(wchar_t *);
_CRTIMP wchar_t * __cdecl wcsset(wchar_t *, wchar_t);
_CRTIMP wchar_t * __cdecl wcslwr(wchar_t *);
_CRTIMP wchar_t * __cdecl wcsupr(wchar_t *);
_CRTIMP int __cdecl wcsicoll(const wchar_t *, const wchar_t *);

#endif  /* !__STDC__ */

#define _WSTRING_DEFINED
#endif

#endif  /* ndef _MAC */

#ifdef  __cplusplus
}
#endif

#endif  /* _INC_STRING */



<string.h>
NULL
    Null pointer constant.
size_t
    Type for objects declared to store result of sizeof operator.

char* strcpy(char* s, const char* ct);
    Copies ct to s including terminating NUL and returns s.

char* strncpy(char* s, const char* ct, size_t n);
    复制最多n个字符给s. ct长度小于n则全部复制(包括NULL结束符).
注意可能离开s而没有结束符. 返回s.

char* strcat(char* s, const char* ct);
    将ct接到s后然后返回s.

strcpy(char destination[], const char source[]);

strcpy:将字符串source拷贝到字符串destination中。
strcpy函数应用举例
原型:strcpy(char destination[], const char source[]);
功能:将字符串source拷贝到字符串destination中
例程:

 #include <iostream.h>
#include <string.h>
void main(void)
{
  char str1[10] = { "TsinghuaOK"};
  char str2[10] = { "Computer"};
  cout <<strcpy(str1,str2)<<endl;
}

运行结果是:Computer

第二个字符串将覆盖掉第一个字符串的所有内容!

注意:在定义数组时,字符数组1的字符串长度必须大于或等于字符串2的字符串长度。不能用赋值语句将一个字符串常量或字符数组直接赋给

一个字符数组。所有字符串处理函数都包含在头文件string.h中。


strncpy(char destination[], const char source[], int numchars);

strncpy:将字符串source中前numchars个字符拷贝到字符串destination中。
strncpy函数应用举例
原型:strncpy(char destination[], const char source[], int numchars);
功能:将字符串source中前numchars个字符拷贝到字符串destination中
例程:

#include <iostream.h>
#include <string.h>
void main(void)
{
  char str1[10] = { "Tsinghua "};
  char str2[10] = { "Computer"};
  cout <<strncpy(str1,str2,3)<<endl;
}

运行结果:Comnghua

注意:字符串source中前numchars个字符将覆盖掉字符串destination中前numchars个字符!

strcat(char target[], const char source[]);

strcat:将字符串source接到字符串target的后面。
strcat函数应用举例
原型:strcat(char target[], const char source[]);
功能:将字符串source接到字符串target的后面
例程:

#include <iostream.h>
#include <string.h>
void main(void)
{
  char str1[] = { "Tsinghua "};
  char str2[] = { "Computer"};
  cout <<strcpy(str1,str2)<<endl;
}

运行结果:Tsinghua Computer

注意:在定义字符数组1的长度时应该考虑字符数组2的长度,因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,字符串1

的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。

strncat(char target[], const char source[], int numchars);

strncat:将字符串source的前numchars个字符接到字符串target的后面。
strncat函数应用举例:
原型:strncat(char target[], const char source[], int numchars);
功能:将字符串source的前numchars个字符接到字符串target的后面
例程:

#include <iostream.h>
#include <string.h>
void main(void)
{
  char str1[] = { "Tsinghua "};
  char str2[] = { "Computer"};
  cout <<strncat(str1,str2,3)<<endl;
}

运行结果:Tsinghua Com


int strcmp(const char firststring[], const char secondstring);

strcmp:比较两个字符串firststring和secondstring。
strcmp函数应用举例
原型:int strcmp(const char firststring[], const char secondstring);
功能:比较两个字符串firststring和secondstring
例程:

#include <iostream.h>
#include <string.h>
void main(void)
{
  char buf1[] = "aaa";
  char buf2[] = "bbb";
  char buf3[] = "ccc";
  int ptr;
  ptr = strcmp(buf2,buf1);
  if(ptr > 0)
   cout <<"Buffer 2 is greater than buffer 1"<<endl;
  else
   cout <<"Buffer 2 is less than buffer 1"<<endl;
  ptr = strcmp(buf2,buf3);
  if(ptr > 0)
   cout <<"Buffer 2 is greater than buffer 3"<<endl;
  else
   cout <<"Buffer 2 is less than buffer 3"<<endl;
}

运行结果是:Buffer 2 is less than buffer 1
                  Buffer 2 is greater than buffer 3

strlen( const char string[] );

strlen:统计字符串string中字符的个数。  
strlen函数应用举例
原型:strlen( const char string[] );
功能:统计字符串string中字符的个数
例程:

#include <iostream.h>
#include <string.h>
void main(void)
{
  char str[100];
  cout <<"请输入一个字符串:";
  cin >>str;
  cout <<"The length of the string is :"<<strlen(str)<<"个"<<endl;
}

运行结果The length of the string is x (x为你输入的字符总数字)

注意:strlen函数的功能是计算字符串的实际长度,不包括'/0'在内。另外,strlen函数也可以直接测试字符串常量的长度,如:strlen

("Welcome")。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值