C + + string parsing __c++

Source: Internet
Author: User
Tags function prototype strcmp strlen strtok
Function Name: strtok
Function prototypes: Char *strtok (char *s1, const char *S2)
function function: Decompose S1 strings into multiple strings separated by a specific delimiter (typically used to break English sentences into words)
function return: string S1 the first occurrence of characters in the S2 before the string pointer
Parameter description: S2 is generally set to the separator character in S1
Specify that the first argument must be NULL when a child call is made (that is, the second to third and subsequent substrings of the split S1)
After each match succeeds, the split substring position in the S1 is replaced with null (the first ring in the chain), so the S1 is corrupted
function remembers the position of the pointer for the next call

File belongs: <string.h>

#include <string.h>
#include <stdio.h>
int main ()
{
Char *p;
Char *buffer;
Char *delims={".,"};

Buffer=strdup ("Find words, all of them.");
printf ("%s\n", buffer);
P=strtok (BUFFER,DELIMS);
while (P!=null) {
printf ("Word:%s\n", p);
P=strtok (NULL,DELIMS);
}
printf ("%s\n", buffer);
return 0;
}



A detailed explanation of the function of string in C language

@ Function Name: strdup
Function prototypes: 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 belongs: <string.h>

#include <stdio.h>
#include <string.h>
#include <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:
File belongs: <string.h>

#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,int count)
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
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
int main ()
{
Char string[10];
Char *str1= "Abcdefghi";
strncpy (string,str1,3);
String[3]= ' ";
printf ("%s", string);
return 0;
}


@ 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:
File belongs: <string.h>

#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 belongs: <string.h>

#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;
}


@ 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, return 0; STR1>STR2, returns a positive number.
Parameter description:
File belongs: <string.h>

#include <string.h> 
#include <stdio.h> 
int main ()  

  Char *buf1= "AAA", *buf2= "BBB", *buf3= "CCC";  
  int ptr; 
  ptr=strcmp (BUF2, buf1);  
& nbsp if (ptr>0)  
    printf ("Buffer 2 is greater than buffer 1\n");  
  else 
    printf ("Buffer 2 is less than buffer 1\n");  
  ptr=strcmp (BUF2, buf3);  
  I F (ptr>0)  
    printf ("Buffer 2 is greater than buffer 3\n");  
  else 
& nbsp;   printf ("Buffer 2 is less than buffer 3\n")  
  return 0; 


@ Function name:   strncmp 
function prototype:   int strncmp (char *str1,char *str2,int count)  
Function:   compares 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, count-the length of the comparison  
File:   < String.h>

#include <string.h> 
#include <stdio.h> 
int main ()  

  INT PTR;&NBSP
   char *buf1= "aaabbb", *buf2= "BBBCCC", *buf3= "CCC";  
  PTR=STRNCMP (BUF2,BUF1, 3);  
  if (ptr>0)  
    printf ("Buffer 2 is greater than buffer 1"); &NBSP
&nb Sp ELSE&NBSP
    printf ("Buffer 2 is less than buffer 1");  
    ptr=strncmp ( buf2,buf3,3);  
  if (ptr>0)  
    printf ("Buffer 2 is greater than Buffer 3"); &NB Sp
  ELSE&NBSP
    printf ("Buffer 2 is less than buffer 3");  
  return (0);  < br>}


@ 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:
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
int main ()
{
Char *p= "Find all vowels";

while (p)
{
printf ("%s\n", p);
P=STRPBRK (p+1, "Aeiouaeiou");
}
return 0;
}


@ 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:
File belongs: <string.h>

#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;
}


@ 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:
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
#include <alloc.h>
int main ()
{
printf ("%d\n", strspn ("out To Lunch", "Aeiou"));
printf ("%d\n", strspn ("out To Lunch", "XYZ"));
return 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>

#include <string.h>
#include <stdio.h>
int main ()
{
Char string[15];
Char *ptr, c= ' R ';
strcpy (String, "This is a string");
PTR=STRCHR (string, c);
if (PTR)
printf ("The character%c. Position:%d\n", c,ptr-string);
Else
printf ("The character is not found\n");
return 0;
}


@ Function Name: STRRCHR
Function prototypes: Char *strrchr (const char *s, int c)
function function: Get the last position pointer containing C character in string s
function return: Position pointer
Parameter description:
File belongs: <string.h>

#include <string.h>
#include <stdio.h>
int main ()
{
Char string[15];
Char *ptr,c= ' r ';
strcpy (String, "This is a string");
PTR=STRRCHR (STRING,C);
if (PTR)
printf ("The character%c. position:%d", c,ptr-string);
Else
printf ("The character is not found");
return 0;
}


@ Function Name: strstr
Function prototype: char* strstr (char* str1,char* str2);
function function: Find out where the STR2 string first appears in the STR1 string (excluding str2 string terminator)
function return: Returns a pointer to the position, if not found, returns a null pointer
Parameter description:
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
int main ()
{
Char *str1= "Open Watcom + + +", *str2= "Watcom", *ptr;
Ptr=strstr (STR1,STR2);
printf ("The substring is:%s\n", PTR);
return 0;
}


@ Function Name: Strrev
Function prototype: Char *strrev (char *s)
function function: Reverse all characters in a string in order
function return: pointer to S
Parameter description:
File belongs: <string.h>

#include <string.h>
#include <stdio.h>
int main ()
{
Char *forward= "string";
printf ("Before Strrev ():%s", forward);
Strrev (forward);
printf ("After Strrev ():%s", forward);
return 0;
}


@ Function Name: Strnset
Function prototypes: Char *strnset (char *s, int ch, size_t N)
function function: Set the first n characters in the string s to the value of CH
function return: pointer to S
Parameter description:
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
int main ()
{
Char *string= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Char letter= ' x ';
printf ("String before Strnset:%s", string);
Strnset (string,letter,13);
printf ("After Strnset:%s", string);
return 0;
}


@ Function Name: Strset
Function prototypes: Char *strset (char *s, int ch)
function function: Set all characters in the string s to the value of CH
function return: pointer to S
Parameter description:
File belongs: <string.h>

#include <stdio.h> 
#include <string.h> 
int main ()  

  Char String[10]= "123456789";  
  Char symbol= ' C ';  
  printf ("Before Strset ():%s", string); &NBSP
  Strset (string, symbol);  
  printf ("After Strset ():%s", string);  
  return 0;&NBSP
}


@ Function Name: strtok
Function prototypes: Char *strtok (char *s1, const char *S2)
function function: Decompose S1 strings into multiple strings separated by a specific delimiter (typically used to break English sentences into words)
function return: string S1 the first occurrence of characters in the S2 before the string pointer
Parameter description: S2 is generally set to the separator character in S1
Specify that the first argument must be NULL when a child call is made (that is, the second to third and subsequent substrings of the split S1)
After each match succeeds, the split substring position in the S1 is replaced with null (the first ring in the chain), so the S1 is corrupted
function remembers the position of the pointer for the next call

File belongs: <string.h>

#include <string.h>
#include <stdio.h>
int main ()
{
Char *p;
Char *buffer;
Char *delims={".,"};

Buffer=strdup ("Find words, all of them.");
printf ("%s\n", buffer);
P=strtok (BUFFER,DELIMS);
while (P!=null) {
printf ("Word:%s\n", p);
P=strtok (NULL,DELIMS);
}
printf ("%s\n", buffer);
return 0;
}


@ Function Name: STRUPR
Function prototype: Char *STRUPR (char *s)
function function: To capitalize characters in the string s
function returns:
Parameter description:
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
int main ()
{
Char *string= "abcdefghijklmnopqrstuvwxyz", *ptr;
PTR=STRUPR (string);
printf ("%s", PTR);
return 0;
}


@ Function Name: strlwr
Function prototype: Char *strlwr (char *s)
Function: Converts characters in a string into lowercase letters
function return: pointer to S
Parameter description:
File belongs: <string.h>

#include <string.h>
int main ()
{
Char str[]= "How to SAY?";
printf ("%s", STRLWR (str));
return 0;
}


@ Function Name: strlen
Function prototype: unsigned int strlen (char * str);
function function: Statistics the number of characters in the string str (excluding Terminator ' ")
function return: Returns the length of the string.
Parameter description:
File belongs: <string.h>

#include <stdio.h>
#include <string.h>
int main ()
{
Char str[]= "How are you!";
printf ("The Lence is:%d\n", strlen (str));
return 0;
}


@ Function Name: strerror
Function prototype: char *strerror (int errnum)
function function: Get the content information of the error message
function return: Error message string pointer
Parameter description: errnum-error number
File belongs: <string.h>

#include <stdio.h>
#include <errno.h>
int main ()
{
Char *buffer;
Buffer=strerror (errno);
printf ("Error:%s", buffer);
return 0;
}


@ Function Name: memcpy
Function prototypes: void *memcpy (void *dest, const void *SRC, size_t N)
function function: string copy
function return: pointer to dest
Parameter description: src-source string, n-Copy maximum length
File belongs: <string.h>,<mem.h>

#include <stdio.h> 
#include <string.h> 
int main ()  

  Char Src[]= "******************************";  
  char dest[]= "abcdefghijlkmnopqrstuvwxyz0123456709"; &NBSP
  Char *ptr; 
  printf ("Destination before memcpy:%s\n", dest);  
  ptr= memcpy (Dest,src,strlen (SRC));  
  if (PTR)  
    printf ("Destination after memcpy :%s\n ", dest);  
  else 
    printf (" memcpy failed ");  
 

Related Article

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.