C language String.h header file function

Source: Internet
Author: User
PS: This article contains a description of most of the strings functions, with an illustrative example. Originally wanted to organize their own, found that there are predecessors have been sorted out, turned over. Modified the source code some of the problems, mainly with the char * literal string problem, causing the program to run when the crash. In addition, you have rewritten part of your test program to make it more responsive to your own testing needs. Improper place, also please Haihan. @ Function Prototype: char *strdup (const char *s)

function function: string copy, destination space allocated by this function

function return: Pointer to the string after the copy

Parameter description: src-the source string to be copied

File: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> #include &      lt;alloc.h> int main () {char *dup_str, *string= "ABCDE";      Dup_str=strdup (string);      printf ("%s", DUP_STR);      Free (DUP_STR);    return 0; }

@ Function Name: strcpy

Function prototype: char* strcpy (char* str1,char* str2);

function function: Copy str2 point string to STR1

function return: Returns STR1, that is, pointer to str1

Parameter description:

Files: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> int main ()      {char string[10];      Char *str1= "Abcdefghi";      strcpy (STRING,STR1);      printf ("The String is:%s\n", string);    return 0; }

@ Function Name: strncpy

Function prototypes: Char *strncpy (char *dest, const char *src,intcount)

function function: Copy the Count characters in the string src to the string dest

function return: pointer to dest

Parameter Description: dest-purpose String, src-source string, count-copy number of characters

Files: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> int main () {char*src = "bbbbbbbbbbbbbbbbbbbb";//20 ' B ' char dest[50] = "aaaaaaaaaaaaaaaaaaaa";//20 ' a ' s puts (d      EST);          strncpy (dest, SRC, 10);        Puts (dest);    Return0; Output: [CPP] view plain copy/******************************************* aaaaaaaaaaaaaaaaaaaa bbbbbbbbbbaaaaa AAAAA *******************************************/Note: strncpy only copy characters of the specified length, not automatically at the end of the "". If the specified length exceeds the length of the source string, the part that is not sufficient to complement '

@ Function Name: strcat

Function prototype: char* strcat (char * str1,char * str2);

function function: str2 The string to the STR1, str1 the final ' "is canceled

function return: str1

Parameter description:

Files: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> int main ()     {char buffer[80];      strcpy (buffer, "Hello");      strcat (Buffer, "world");      printf ("%s\n", buffer);    return 0; }

@ Function Name: strncat

Function prototypes: Char *strncat (char *dest, const char *SRC, size_t maxlen)

function function: Fonts The MaxLen word in the string src to dest

function returns:

Parameter description:

File: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> char buffer   [80];      int main () {strcpy (buffer, "Hello");      Strncat (Buffer, "world", 8);      printf ("%s\n", buffer);      Strncat (buffer, "*************", 4);      printf ("%s\n", buffer);    return 0; Note: Unlike strncpy, Strncat automatically adds ' "At the end, and if the specified length exceeds the length of the source string, only the length of the source string is stopped

@ Function Name: strcmp

Function prototype: int strcmp (char * str1,char * str2);

function function: Compares two string str1,str2.

function returns: STR1<STR2, returns a negative number, STR1=STR2, returns a 0;STR1>STR2, returns a positive number.

Parameter description:

File belongs: <string.h>[CPP] View plain copy #include  <string.h>    #include  <stdio.h>    Int main ()     {      char *buf1= "AAA",  *buf2= "BBB", *buf3= "CCC";      int ptr;      ptr=strcmp (BUF2,&NBSP;BUF1);       if (ptr>0)         printf ("buffer 2  is greater thanbuffer 1\n ");      else         printf ("buffer 2 is less thanbuffer 1\n");      &NBSP;PTR=STRCMP (BUF2,&NBSP;BUF3);      if (ptr>0)          printf ("buffer 2 is greater thanbuffer 3\n");       else        printf ("Buffer 2 is less thanbuffer  3\n ");       return 0;   }  

@ Function Name: strncmp

function prototypes: int strncmp (char *str1,char *str2,int count)

function function: Compare the first count characters in str1 and str2 by dictionary order

function returns: Less than 0:STR1<STR2, equal to 0:STR1=STR2, greater than 0:STR1>STR2

Parameter description: str1,str2-the string to compare, the length of the count-comparison

File belongs: <string.h>[CPP] View plain copy #include <string.h>    #include <stdio.h>    int main ( )     {       char str1[] = "AABBC";//       char str2[] =  "ABBCD";//      //to make the test program more concise, this assumes that strncmp only returns-1 , 0, 13       char res_info[] = {' < ', ' = ', ' > '};       int res;          //First 1 characters comparison       &NBSP;RES&NBSP;=&NBSP;STRNCMP (str1, str2, 1);      printf ("1:str1%c  Str2\n ",  res_info[res+1]);            //first 3 characters comparison     &NBSP;&NBSP;&NBSP;RES&NBSP;=&NBSP;STRNCMP (str1, str2, 3);      printf ( "3:str1%c str2\n",  res_info[res+1]);  }  

Output: [CPP] view plain copy/**************************************** 1:str1= str2 3:str1< str2 ****************************/

@ Function Name: strpbrk

Function prototypes: Char *strpbrk (const char *S1, const char *S2)

Function: Gets the position pointer of the first "also in S2" character in S1

function return: Position pointer

Parameter description:

Files: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> int main ()          {char *p= "find all vowels";      P=STRPBRK (p+1, "Aeiouaeiou");         while (p) {printf ("%s\n", p);              P=STRPBRK (p+1, "Aeiouaeiou");    return 0; }

Output: [CPP] view PLAIN copy/************************************** ind all vowels all vowels owels ********************************/

@ Function Name: strcspn

function prototypes: int strcspn (const char *S1, const char *S2)

function function: Statistics The length of S1 from beginning to end until the first "character from S2" appears

function return: Length

Parameter description:

Files: <string.h> [CPP] view plain copy #include <stdio.h> #include <string.h> int main ()     {printf ("%d\n", Strcspn ("Abcbcadef", "CBA"));     printf ("%d\n", Strcspn ("Xxxbcadef", "CBA"));      printf ("%d\n", Strcspn ("123456789", "CBA"));    return 0; }

Output: [CPP] view Plain copy/************************ 0 3 9 ************************/

@ Function Name: strspn

function prototypes: int strspn (const char *S1, const char *S2)

function function: Statistics The length of S1 from beginning to end until the first "character not from S2" appears

function return: Position pointer

Parameter description:

Owning file: <string.h> [HTML] view plain copy #include <stdio.h> #include <string.h> #include      ;alloc.h> int main () {printf ("%d\n", Strspn ("Abcbcadef", "CBA"));      printf ("%d\n", Strspn ("Xxxbcadef", "CBA"));      printf ("%d\n", Strspn ("123456789", "CBA"));    return 0; Output: [CPP] view plain copy/************************ 6 0 0 ************************/

@ Function Name: STRCHR

Function prototype: char* STRCHR (char* str,char ch);

function function: Finds the first occurrence of the character ch in the string that str points to

function return: Returns a pointer to the position, if it cannot be found, returns a null pointer

Parameter description: str-to search for the string, ch-lookup characters

File belongs: <string.h>[CPP] View plain copy #include <string.h>    #include <stdio.h>    int main ( )     {       char *str =  "this is a  String! ";       char ch;      char *p;           while (1)       {          printf ("Please input a char:");         ch =  getchar ();         p = strchr (str, ch);          if (p)              printf ("%c is the %d character of\"%s\ "\ n",ch,  (int) (p-str+1), str);          else            printf ("not found!\n");             printf (" press esc to quit!\n\n ");         if (27 ==  Getch ())             break;          fflush (stdin);      }          return 0;   }  

Run Results: [CPP] view plain copy/********************************************  please  input achar:i  I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.