string.h详解

来源:互联网 发布:紫鸟数据魔方可靠吗 编辑:程序博客网 时间:2024/06/04 19:03

1.memchr

void * memchr ( void * ptr, int value, size_t num );

功能:从str指内存区域的前num个字节查找字符value。

说明:当第一次遇到字符value时停止查找。如果成功,返回指向字符value的指针;否则返回NULL。

#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;}
Output:'p' found at position 5.

memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

ptr1是字符串1;ptr2是字符串2;num是对比的字数。

功能:memcmp是比较内存区域buf1和buf2的前count个字节。该函数是按字节比较的。

说明:当buf1<buf2时,返回值小于0当buf1==buf2时,返回值=0当buf1>buf2时,返回值大于0
/* memcmp example */#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;}
Output:'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.

memcpy

void * memcpy ( void * destination, const void * source, size_t num );

功能:将source复制到destination,num是source的长度。

#include <stdio.h>#include <string.h>struct {  char name[40];  int age;} person, person_copy;int main (){  char myname[] = "Pierre de Fermat";  /* using memcpy to copy string: */  memcpy ( person.name, myname, strlen(myname)+1 );  person.age = 46;  /* using memcpy to copy structure: */  memcpy ( &person_copy, &person, sizeof(person) );  printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );  return 0;}
Output:person_copy: Pierre de Fermat, 46 

memmove

void * memmove ( void * destination, const void * source, size_t num );

功能:将source开始的num个字符赋值到destination。

#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;}
Output:memmove can be very very useful.

和memcpy什么区别呢?

#include <stdio.h>#include <string.h>int main(){    char str[] = "123456789";    memmove(str+5, str,3);    puts(str);    getchar();    return 0;}
Output:123451239

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);  char str[] = "almost every programmer should know memset!";  memset (str,'-',6);  puts (str);  return 0;}
output:------ every programmer should know memset!

strcat

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

功能:将source加到destination后面。

#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. 

strchr

const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
功能:在str中字符串中找character,找到则返回地址,否则返回NULL;

#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;}
Output:Looking for the 's' character in "This is a sample string"...found at 4found at 7found at 11found at 18

strcmp

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

功能:对比,类似memcmp

#include <stdio.h>#include <string.h>int main (){  char key[] = "apple";  char buffer[80];  do {     printf ("Guess my favorite fruit? ");     fflush (stdout);     scanf ("%79s",buffer);  } while (strcmp (key,buffer) != 0);  puts ("Correct answer!");  return 0;}
Output:Guess my favourite fruit? orangeGuess my favourite fruit? appleCorrect answer!

strcoll

int strcoll ( const char * str1, const char * str2 );
没研究清楚。

strcpy

char * strcpy ( char * destination, const char * source );
功能:将source复制到destination

#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 stringstr2: Sample stringstr3: copy successful

strcspn

size_t strcspn ( const char * str1, const char * str2 );
功能:如果str1和str2中有相同的字符,那么输出位置,否则返回长度(strlen(str1));
说明:strpbrk和strcspn这两个函数功能相似,区别在于前者返回的是一个指针,而后而返回的是一个数组下标值

#include <stdio.h>#include <string.h>int main (){  char str[] = "fcba73";  char keys[] = "1234567890";  int i;  i = strcspn (str,keys);  printf ("The first number in str is at position %d.\n",i+1);  return 0;}
Output:The first number in str is at position 5

strlen

size_t strlen ( const char * str );
功能:返回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;}
Output:Enter sentence: just testingThe sentence entered is 12 characters long.

strncat

char * strncat ( char * destination, const char * source, size_t num );
功能:将source的前num个字符添加到destination后面,返回destination。

#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;}
Output:To be or not

strncmp

int strncmp ( const char * str1, const char * str2, size_t num );
功能:在str1中查找是否有str2中的前num个字符,返回如strcmp。

#include <stdio.h>#include <string.h>int main (){  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };  int n;  puts ("Looking for R2 astromech droids...");  for (n=0 ; n<3 ; n++)    if (strncmp (str[n],"R2xx",2) == 0)    {      printf ("found %s\n",str[n]);    }  return 0;}
Output:Looking for R2 astromech droids...found R2D2found R2A6

strncpy

char * strncpy ( char * destination, const char * source, size_t num );
功能:将source的前num个字符复制到destination。

#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;}
Output:To be or not to beTo be or not to beTo be 

strpbrk

const char * strpbrk ( const char * str1, const char * str2 );
char * strpbrk ( char * str1, const char * str2 );
功能:如果str1和str2中有相同的字符,那么输出位置,否则返回长度(strlen(str1));
说明:strpbrk和strcspn这两个函数功能相似,区别在于前者返回的是一个指针,而后而返回的是一个数组下标值。

#include <stdio.h>#include <string.h>int main (){  char str[] = "This is a sample string";  char key[] = "aeiou";  char * pch;  printf ("Vowels in '%s': ",str);  pch = strpbrk (str, key);  while (pch != NULL)  {    printf ("%c " , *pch);    pch = strpbrk (pch+1,key);  }  printf ("\n");  return 0;}
Output:Vowels in 'This is a sample string': i i a a e i 

strrchr

const char * strrchr ( const char * str, int character );
char * strrchr ( char * str, int character );
功能:查找一个字符串在另一个字符串中 末次 出现的位置,并返回地址,否则返回NULL;

#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;}
Output:Last occurrence of 's' found at 18

strspn

size_t strspn ( const char * str1, const char * str2 );
功能:

#include <stdio.h>#include <string.h>int main (){  int i;  char strtext[] = "129th";  char cset[] = "1234567890";  i = strspn (strtext,cset);  printf ("The initial number has %d digits.\n",i);  return 0;}
Output:The initial number has 3 digits.

strstr

const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
功能:str2是否是str1的子串。

#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;}
Output:This is a sample string

strtok

char * strtok ( char * str, const char * delimiters );
功能:切割字符串,将str切分成一个个子串,delimiters作为分割点
函数返回值:
  当s中的字符查找到末尾时,返回NULL;
  如果查不到delimiter所标示的字符,则返回当前strtok的字符串的指针。

#include <stdio.h>#include <string.h>int main (){  char str[] ="- This, a sample string.";  char * pch;  printf ("Splitting string \"%s\" into tokens:\n",str);  pch = strtok (str," ,.-");  while (pch != NULL)  {    printf ("%s\n",pch);    pch = strtok (NULL, " ,.-");  }  return 0;}
Output:Splitting string "- This, a sample string." into tokens:Thisasamplestring

strxfrm

spn没写完

原创粉丝点击