C Language String Operations Daquan

Source: Internet
Author: User
Tags alphabetic character first string strcmp uppercase character

reprinted from: C-Language String Operations Summary (super verbose)

1) String manipulation
strcpy (P, p1) copy string
strncpy (P, p1, N) copies the specified length string
Strcat (P, p1) Append string
Strncat (P, p1, N) append a specified length string
Strlen (p) Take string length
strcmp (P, p1) compare strings
STRCASECMP ignoring case comparison strings
STRNCMP (P, p1, N) compares a specified length string
STRCHR (P, c) to find the specified character in a string
STRRCHR (P, c) reverse lookup in string
Strstr (P, p1) Find string
STRPBRK (P, p1) finds any element of the collection in the current string as a collection of all the characters of the target string
STRSPN (P, p1) takes all the characters of the target string as a collection, and finds the offset of any element that does not belong to the collection in the current string
STRCSPN (P, p1) takes all the characters of the target string as a collection, and finds the offset of any element belonging to the collection in the current string
* String handler with the specified length fills the 0 end character after the processed string

2) Conversion of string to numeric type
Strtod (P, ppend) converts a double type value from string p and stores subsequent string pointers to the char* type store that ppend points to.
Strtol (P, ppend, base) converts a long type integer value from the string p, base explicitly sets the integer binary of the conversion, set to 0 to determine the input, 0x, 0X prefix to be interpreted according to a specific format, and the 0 prefix to interpret as an integer in octal format Type
Atoi (p) string conversion to int integer
Atof (p) string conversion to double character points
Atol (p) string conversion to Long integer

3) Character check
Isalpha () Check if it is an alphabetic character
Isupper () Check if it is an uppercase character
Islower () Check if lowercase alphabetic characters
IsDigit () Check if it is a number
Isxdigit () Check if valid characters are represented as hexadecimal digits
Isspace () checks if the character is a space type
Iscntrl () Check if the control character
ISPUNCT () Check if it is a punctuation mark
Isalnum () Check for letters and numbers
Isprint () Check if it is a printable character
Isgraph () Check if it is a graphic character, equivalent to Isalnum () | Ispunct ()

4) Function prototypes
Prototype: strcpy (char destination[], const char source[]);
Function: Copy the string source to the string destination
Routines:
#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str1[10] = {"Tsinghuaok"};
Char str2[10] = {"Computer"};
cout <<strcpy (STR1,STR2) <<endl;
}

The operating result is: computer
The second string will overwrite all the contents of the first string!
Note: When you define an array, the string length of the character array 1 must be greater than or equal to the string length of String 2. You cannot assign a string constant or an array of characters directly to an array of characters using an assignment statement. All string handler functions are included in the header file string.h.


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

strncpy: Copies the first numchars characters in the string source to the string destination.
Example of application of strncpy function
Prototype: strncpy (char destination[], const char source[], int numchars);
Function: Copies the first numchars characters in the string source into the string destination
Routines:

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

Running Result: Comnghua
Note: The first numchars characters in the string source will overwrite the first numchars characters in the string destination!

Prototype: strcat (char target[], const char source[]);
Function: string source to the back of the string target
Routines:
#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str1[] = {"Tsinghua"};
Char str2[] = {"Computer"};
cout <<strcpy (STR1,STR2) <<endl;
}

Running Result: Tsinghua computer

Note: You should consider the length of the character array 2 when defining the length of the character array 1, because the length of the new string is the sum of two string lengths after the connection. After the string is concatenated, the end character of the string 1 is automatically removed and a trailing character after the new string is retained at the end of the end string.

Prototype: Strncat (char target[], const char source[], int numchars);
Function: NumChars The first character of the string source to the back of the string target
Routines:

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

Run Result: Tsinghua Com


Prototype: int strcmp (const char firststring[], const char secondstring);
Function: Compare two strings of firststring and secondstring
Routines:

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

The operation results are: Buffer 2 is less than Buffer 1
Buffer 2 is greater than buffer 3


Prototype: strlen (const char string[]);
Function: Counts the number of characters in a string of strings
Routines:

#include <iostream.h>
#include <string.h>
void Main (void)
{
Char str[100];
cout << "Please enter a string:";
CIN >>str;
cout << "The length of the string is:" <<strlen (str) << "x" <<endl;
}

Run result the length of the string is x (x for the total number of characters you enter)

Note: The function of the Strlen function is to calculate the actual length of the string, not including '/'. In addition, the strlen function can also directly test the length of a string constant, such as: strlen ("Welcome").


void *memset (void *dest, int c, size_t count);
Dest the preceding count characters to character C. Returns the value of the dest.

void *memmove (void *dest, const void *SRC, size_t count);
Copies the count byte characters from SRC to dest.  If SRC and dest overlap, the function is processed automatically. Returns the value of the dest.

void *memcpy (void *dest, const void *SRC, size_t count);
Copies the count byte characters from SRC to dest.  Just as with the Memmove function, there is no way to handle the overlap of src and dest. Returns the value of the dest.

void *memchr (const void *buf, int c, size_t count);
Finds the first occurrence of the character C in the count byte in front of BUF. The character C was found or the count byte has been searched, and the lookup stops. Successful operation returns the position pointer for the first occurrence of C in buf, otherwise returns NULL.

void *_memccpy (void *dest, const void *SRC, int c, size_t count);
Copies 0 or more bytes of characters from Src to dest. When character c is copied or count characters are copied, replication stops.

If the character c is copied, the function returns a pointer to the character position immediately following the character. Otherwise, NULL is returned.

int memcmp (const void *BUF1, const void *BUF2, size_t count);
Compare BUF1 and Buf2 in front of Count byte size.
The return value is < 0, indicating that BUF1 is less than buf2;
The return value is 0, indicating that buf1 equals buf2;
The return value of > 0 indicates that BUF1 is greater than buf2.

int memicmp (const void *BUF1, const void *BUF2, size_t count);

Compare BUF1 and Buf2 in front of Count bytes. Unlike memcmp, it is not case-sensitive.

The return value is ibid.

Char *strrev (char *string);
Reverses the order of characters in a string of strings.  The null terminator position does not change. Returns a pointer to the adjusted string.

Char *_strupr (char *string);
Replaces all lowercase letters in a string with the corresponding uppercase letters, and the other characters remain unchanged. Returns a pointer to the adjusted string.

Char *_strlwr (char *string);
Replaces all uppercase letters in a string with the corresponding lowercase letters, and the other characters remain unchanged. Returns a pointer to the adjusted string.

Char *strchr (const char *string, int c);
Finds the first occurrence in a string string, and the null terminator is also included in the lookup. Returns a pointer to the position where the character C first appears in a string, or null if it is not found.

Char *strrchr (const char *string, int c);
Finds the last occurrence of a character, C, in a string, that is, an inverse search for a string that contains a null terminator.
Returns a pointer to the last occurrence of the character C in a string, or null if not found.

Char *strstr (const char *string, const char *strsearch);
Finds the Strsearch substring in a string of strings. Returns a pointer to the first occurrence of a substring of strsearch in a string. If no substring strsearch is found, NULL is returned. If the substring strsearch is an empty string, the function returns a string value.

Char *strdup (const char *strsource);
The function runs itself by invoking the malloc function to allocate storage space for the copied strsource string and then copying the strsource into the allocated space. Be careful to release this allocated space in a timely manner.
Returns a pointer to the space allocated for the copied string; If the allocation space fails, a null value is returned.

Char *strcat (char *strdestination, const char *strsource);
Add the source string strsource to the target string strdestination, followed by a null terminator after the resulting new string. The character of the source string strsource overwrites the Terminator null after the target string strdestination. There is no overflow check during the copy or add of the string, so make sure the target string space is large enough.  Cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strncat (char *strdestination, const char *strsource, size_t count);
Adds count characters from the beginning of the source string strsource to the target string strdest. The character of the source string strsource overwrites the Terminator null after the target string strdestination. If count is greater than the source string length, the count value is replaced with the length value of the source string. The resulting new string is automatically appended with a null terminator. As with the STRCAT function, this function cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strcpy (char *strdestination, const char *strsource);
Copies the source string strsource to the location specified by the target string strdestination, including the null terminator. Cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strncpy (char *strdestination, const char *strsource, size_t count);
Copies the count characters from the beginning of the source string strsource to the location specified by the target string strdestination. If the count value is less than or equal to the length of the strsource string and the null terminator is not automatically added in the target string, and count is greater than the length of the strsource string, the strsource is padded with a null terminator to fill the count characters and copied to the target string. Cannot handle the case where the source string overlaps with the target string. The function returns the Strdestination value.

Char *strset (char *string, int c);
Sets all characters of string string to character C and encounters a null terminator stop. The function returns a string pointer after the content is adjusted.

Char *strnset (char *string, int c, size_t count);
The string string starts with count characters to character C, and if the count value is greater than the length of a string string, the count value is replaced with the length of string. The function returns a string pointer after the content is adjusted.

size_t strspn (const char *string, const char *strcharset);
Finds the ordinal number of the first occurrence of any character (except the string terminator null) that is not contained in the strCharSet string in the string string. Returns an integer value that specifies the length of a substring that consists of all the characters in characters in a string. If the string starts with a character that is not contained in strCharSet, the function returns a value of 0.

size_t strcspn (const char *string, const char *strcharset);
Finds the ordinal number of the first occurrence of any character in the strCharSet string in a string string, containing the string terminator null.
Returns an integer value that specifies the length of a substring that consists of all characters in a string that are not characters. If the string starts with a character contained in strCharSet, the function returns a value of 0.

Char *strspnp (const char *string, const char *strcharset);
Finds any position pointer that is not included in the strCharSet string (except for the string terminator null) in the string string for the first occurrence. Returns a pointer to the position of the character in the non-strcharset in the first occurrence of the string.

Char *strpbrk (const char *string, const char *strcharset);
Finds the position of any character in the strCharSet string for the first time in a string string and does not contain a string terminator null.
Returns a pointer to the position where any character in the strCharSet first appears in a string. If two string arguments do not contain the same character, a null value is returned.

int strcmp (const char *string1, const char *string2);
Compares string string1 and string2 size.
The return value is < 0, indicating that string1 is less than string2;
The return value is 0, indicating that string1 equals string2;
The return value of > 0 indicates that string1 is greater than string2.

int stricmp (const char *string1, const char *string2);
Compare string string1 and string2 sizes, unlike strcmp, which compare their lowercase versions. The return value is the same as strcmp.

int Strcmpi (const char *string1, const char *string2);
is equivalent to the STRICMP function, but provides a backward-compatible version.

int strncmp (const char *string1, const char *string2, size_t count);
Compares the string string1 and string2 size, comparing only the preceding count characters. During the comparison, the length of any one string is less than count, and count is replaced by the length of the shorter string. At this point, if the characters in front of the two strings are equal, the shorter string is smaller.
A return value of < 0, indicating that the substring of the string1 is less than the substring of string2;
A return value of 0 indicates that the substring of string1 is equal to the substring of string2;
The return value, > 0, indicates that the substring of the string1 is greater than the string2 string.

int strnicmp (const char *string1, const char *string2, size_t count);
Compares the string string1 and string2 size, comparing only the preceding count characters.  Unlike strncmp, they are compared in lowercase versions of their letters. The return value is the same as strncmp.

Char *strtok (char *strtoken, const char *strdelimit);
Finds the next tag in the Strtoken string, and the Strdelimit character set specifies the delimiter that may be encountered in the current lookup call. Returns a pointer to the next tag found in Strtoken. If the token is not found, a null value is returned. Each call modifies the strtoken content, replacing each delimiter encountered with a null character.

C Language String Operations Daquan

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.