C++ STL之 #include <string>头文件

在字符串头文件string下有很多常用的方法,主要包括:

<1> 复制

(1) memcpy

函数原型 void * memcpy ( void * destination, const void * source, size_t num );

参数

destination: 目标字符串

source: 待复制的字符串

size_t: 需要复制的长度

#include <stdio.h>
#include <string.h>

int main ()
{
    //待复制的字符串
  char str1[] = "almost every programmer should know memset!";
    //目标字符串
  char str2[] = "I LOVE SHANGHAI";
    //将待复制的字符串的前十个字符复制到目标字符串中
  memcpy(str2, str1, 10);
    // 输出复制后的目标字符串
  puts(str2);
  return 0;
}    
执行结果:almost eveNGHAI

(2)memmove

函数原型 void * memmove ( void * destination, const void * source, size_t num );

功能:将待移除字符串中的前size_t个字符移动到目标字符串后面

参数

destination: 目标字符串

source: 待移动的字符串

size_t: 需要移动的长度

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "memmove can be very useful......";
  memmove (str+20,str+15,11);
  puts (str);
  return 0;
}
执行结果:memmove can be very very useful.

(3)strcpy

函数原型 char * strcpy ( char * destination, const char * source );

参数

destination:目标字符串

source:源字符串

#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;
}
执行结果:
str1: Sample string

str2: Samplestring
str3: copy successful

(4)strncpy

函数原型 char * strncpy ( char * destination, const char * source, size_t num );

参数

destination:目标字符串

source:源字符串

size_t:复制源字符串size_t长度字符

#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]= "To be or not to be";
  char str2[40];
  char str3[40];

  /* copy to sized buffer (overflow safe): */
  strncpy ( str2, str1, sizeof(str2) );

  /* partial copy (only 5 chars): */
  strncpy ( str3, str2, 5 );
  str3[5] = '\0';   /* null character manually added */

  puts (str1);
  puts (str2);
  puts (str3);

  return 0;
}
执行结果:
To be or not to be
To be or not to be
To be

<2> 连接

(1)strcat

函数原型 char * strcat ( char * destination, const char * source );

参数

destination:目标字符串

source:源字符串

#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;
}
执行结果:these strings are concatenated.

(2)strncat

函数原型 char * strncat ( char * destination, const char * source, size_t num );

参数

destination:目标字符串

source:源字符串

size_t:源字符串中需要连接的长度

#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[20];
  char str2[20];
  strcpy (str1,"To be ");
  strcpy (str2,"or not to be");
  strncat (str1, str2, 6);
  puts (str1);
  return 0;
}
执行结果:To be or not

 <3> 比较

(1)memcmp

函数原型 int memcmp ( const void * ptr1, const void * ptr2, size_t num );

#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=memcmp ( buffer1, buffer2, sizeof(buffer1) );

  if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}
执行结果:'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.

(2) strcmp

函数原型 int strcmp ( const char * str1, const char * str2 );

#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=strcmp( buffer1, buffer2);

  if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}
执行结果:'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.

(3)strncmp

函数原型 int strncmp ( const char * str1, const char * str2, size_t num );

#include <stdio.h>
#include <string.h>

int main ()
{
  char buffer1[] = "DWgaOtP12df0";
  char buffer2[] = "DWGAOTP12DF0";

  int n;

  n=strncmp( buffer1, buffer2, 2);

  if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
  else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
  else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);

  return 0;
}
执行结果:'DWgaOtP12df0' is the same as 'DWGAOTP12DF0'.

<4> 查找

(1)memchr

函数原型 void * memchr ( void * ptr, int value, size_t num );

#include <stdio.h>
#include <string.h>

int main ()
{
  char * pch;
  char str[] = "Example string";
  pch = (char*) memchr (str, 'p', strlen(str));
  if (pch!=NULL)
    printf ("'p' found at position %d.\n", pch-str+1);
  else
    printf ("'p' not found.\n");
  return 0;
}
执行结果:'p' found at position 5.

(2)strchr

函数原型:char * strchr ( char * str, int character );

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}
执行结果:
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18

(3)strrchr

函数原型 char * strrchr ( char * str, int character );

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  pch=strrchr(str,'s');
  printf ("Last occurence of 's' found at %d \n",pch-str+1);
  return 0;
}
执行结果:Last occurrence of 's' found at 18

(4)strstr

函数原型 char * strstr ( char * str1, const char * str2 );

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}
执行结果:This is a sample string

<5> 其他

(1)memset 

函数原型 void * memset ( void * ptr, int value, size_t num );

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}
执行结果:------ every programmer should know memset!

(2)strlen

函数原型 size_t strlen ( const char * str );

#include <stdio.h>
#include <string.h>

int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
  return 0;
}
执行结果:
Enter sentence: just testing
The sentence entered is 12 characters long.

 

posted on 2020-06-02 15:02  天池怪侠  阅读(3692)  评论(0编辑  收藏  举报

导航