C string operation

Source: Internet
Author: User
Tags strtok
C string operation (conversion) Note: several case-insensitive comparison functions in the text use stricmp in the original article. Later I found that the STD library in Linux is not available and changed to the strcasecmp series.


Function Name: strcpy
Function: copy a string to another string.
Usage: char * strcpy (char * Destin, char * Source );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void ){
Char string [10];
Char * str1 = "abcdefghi ";
Stpcpy (string, str1 );
Printf ("% s/n", string );
Return 0;
}
Function Name: strcat
Function: String concatenation Function
Usage: char * strcat (char * Destin, char * Source );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void ){
Char destination [25];
Char * blank = "", * c = "C ++", * Borland = "Borland ";
Strcpy (destination, Borland );
Strcat (destination, blank );
Strcat (destination, C );
Printf ("% s/n", destination );
Return 0;
}
Function Name: strchr
Function: Find the first matching position of a given character in a string/
Usage: char * strchr (char * STR, char C );
Program example:
# I nclude <string. h> # I nclude <stdio. h>
Int main (void ){
Char string [15];
Char * PTR, c = 'R ';
Strcpy (string, "This is a string ");
PTR = strchr (string, C );
If (PTR)
Printf ("the character % C is at position: % d/N", C, PTR-string );
Else
Printf ("the character was not found/N ");
Return 0;
}
Function Name: strcmp
Function: String comparison
Usage: int strcmp (char * str1, char * str2 );
View ASIC code, str1> str2, return value> 0; two strings are equal, return 0 program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void ){
Char * buf1 = "AAA", * buf2 = "BBB", * buf3 = "CCC ";
Int PTR;
PTR = strcmp (buf2, buf1 );
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 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 3/N ");
Else
Printf ("Buffer 2 is less than buffer 3/N ");
Return 0;
}
Function Name: strncmpi
Function: compares a part of a string with another string regardless of case.
Usage: int strncmpi (char * str1, char * str2, unsigned maxlen );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * buf1 = "BBB", * buf2 = "BBB ";
Int PTR;
PTR = strcmpi (buf2, buf1 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1/N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1/N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1/N ");
Return 0;
}
Function Name: strcpy
Function: Copy strings
Usage: char * strcpy (char * str1, char * str2 );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void)
{
Char string [10];
Char * str1 = "abcdefghi ";
Strcpy (string, str1 );
Printf ("% s/n", string );
Return 0;
}
Function Name: strcspns
Function: Search for the content segment of the first given character set in the string.
Usage: int strcspn (char * str1, char * str2 );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
# I nclude <alloc. h>
Int main (void ){
Char * string1 = "1234567890 ";
Char * string2 = "747dc8 ";
Int length;
Length = strcspn (string1, string2 );
Printf ("character where strings intersect is at position % d/N", length );
Return 0;
}
Function Name: strdup
Function: copy the string to the new position.
Usage: char * strdup (char * Str );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
# I nclude <alloc. h>
Int main (void)
{
Char * dup_str, * string = "ABCDE ";
Dup_str = strdup (string );
Printf ("% s/n", dup_str );
Free (dup_str );
Return 0;
}
Function Name:
Strcasecmp

Function: Compares two strings in upper and lower case insensitive mode.
Usage: int
Strcasecmp (char * str1, char * str2 );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * buf1 = "BBB", * buf2 = "BBB ";
Int PTR;
PTR =
Strcasecmp (buf2, buf1 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1/N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1/N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1/N ");
Return 0;
}
Function Name: strerror
Function: returns a pointer to the error message string.
Usage: char * strerror (INT errnum );
Program example:
# I nclude <stdio. h>
# I nclude <errno. h>
Int main (void)
{
Char * buffer;
Buffer = strerror (errno );
Printf ("error: % s/n", buffer );
Return 0;
}
Function Name:
Strcasecmp
Function: compares a string with another string, regardless of case.
Usage: int
Strcasecmp (char * str1, char * str2 );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * buf1 = "BBB", * buf2 = "BBB ";
Int PTR;
PTR =
Strcasecmp (buf2, buf1 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1/N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1/N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1/N ");
Return 0 ;}
Function Name: strncmp
Function: String comparison
Usage: int strncmp (char * str1, char * str2, int maxlen );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * buf1 = "aaabbb", * buf2 = "bbbccc", * buf3 = "CCC ";
Int PTR;
PTR = strncmp (buf2, buf1, 3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1/N ");
Else
Printf ("Buffer 2 is less than buffer 1/N ");
PTR = strncmp (buf2, buf3, 3 );

If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 3/N ");
Else
Printf ("Buffer 2 is less than buffer 3/N ");

Return (0 );
}
Function Name:
Strncasecmp
Function: compares a part of a string with a part of another string, regardless of case.
Usage: int
Strncasecmp (char * str1, char * str2 );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * buf1 = "bbbccc", * buf2 = "bbbccc ";
Int PTR;
PTR =
Strncasecmp (buf2, buf1, 3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1/N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1/N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1/N ");
Return 0;
}
Function Name: strncpy
Function: Copy strings
Usage: char * strncpy (char * Destin, char * Source, int maxlen );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void)
{
Char string [10];
Char * str1 = "abcdefghi ";
Strncpy (string, str1, 3 );
String [3] = '/0 ';
Printf ("% s/n", string );
Return 0;
}
Function Name: strnicmp
Function: Compares two strings case-insensitive.
Usage: int strnicmp (char * str1, char * str2, unsigned maxlen );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * buf1 = "bbbccc", * buf2 = "bbbccc ";
Int PTR;
PTR = strnicmp (buf2, buf1, 3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1/N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1/N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1/N ");
Return 0;
}
Function Name: strnset
Function: Set all characters in a string to a specified character.
Usage: char * strnset (char * STR, char CH, unsigned N );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void)
{
Char * string = "abcdefghijklmnopqrstuvwxyz ";
Char letter = 'X ';
Printf ("string before strnset: % s/n", string );
Strnset (string, letter, 13 );
Printf ("string after strnset: % s/n", string );
Return 0;
}
Function Name: strpbrk
Function: searches for characters in a given character set in a string.
Usage: char * strpbrk (char * str1, char * str2 );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void)
{
Char * string1 = "abcdefghijklmnopqrstuvwxyz ";
Char * string2 = "onm ";
Char * PTR;
PTR = strpbrk (string1, string2 );
If (PTR)
Printf ("strpbrk found first character: % C/N", * PTR );
Else
Printf ("strpbrk didn't find character in set/N ");
Return 0;
}
Function Name: strrchr
Function: Find the last occurrence of a specified character in the string.
Usage: char * strrchr (char * STR, char C );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char string [15];
Char * PTR, c = 'R ';
Strcpy (string, "This is a string ");
PTR = strrchr (string, C );
If (PTR)
Printf ("the character % C is at position: % d/N", C, PTR-string );
Else
Printf ("the character was not found/N ");
Return 0;
}
Function Name: strrev
Power: String reversal
Usage: char * strrev (char * Str );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char * Forward = "string ";
Printf ("before strrev (): % s/n", forward );
Strrev (forward );
Printf ("after strrev (): % s/n", forward );
Return 0;
}
Function Name: strset
Function: Set all characters in a string to a specified character.
Usage: char * strset (char * STR, char C );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void ){
Char string [10] = "123456789 ";
Char symbol = 'C ';
Printf ("before strset (): % s/n", string );
Strset (string, symbol );
Printf ("after strset (): % s/n", string );
Return 0;
}
Function Name: strspns
Function: finds the first occurrence of a subset of a specified character set in a string.
Usage: int strspn (char * str1, char * str2 );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
# I nclude <alloc. h>
Int main (void)
{
Char * string1 = "1234567890 ";
Char * string2 = "123dc8 ";
Int length;
Length = strspns (string1, string2 );
Printf ("character where strings differ is at position % d/N", length );
Return 0;
}
Function Name: strstr
Function: Search for the first appearance of a specified string in the string.
Usage: char * strstr (char * str1, char * str2 );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void)
{
Char * str1 = "Borland International", * str2 = "nation", * PTR;
PTR = strstr (str1, str2 );
Printf ("the substring is: % s/n", PTR );
Return 0 ;}
Function Name: strtodd
Function: converts a string to a double value.
Usage: Double strtodd (char * STR, char ** endptr );
Program example:
# I nclude <stdio. h>
# I nclude <stdlib. h>
Int main (void)
{
Char input [80], * endptr;
Double;
Printf ("enter a floating point number :");
Gets (input );
= Strtodd (input, & endptr );
Printf ("the string is % s the number is % lf/N", input ,);
Return 0;
}
Function Name: strtok
Function: searches for words separated by the delimiters specified in the second string.
Usage: char * strtok (char * str1, char * str2 );
Program example:
# I nclude <string. h>
# I nclude <stdio. h>
Int main (void)
{
Char input [16] = "ABC, D ";
Char * P;
/* Strtok places a null terminator in front of the token, if found */
P = strtok (input ,",");
If (P)
Printf ("% s/n", P );
/* A second call to strtok using a null
As the first parameter returns a pointer
To the character following the token
*/
P = strtok (null ,",");
If (P)
Printf ("% s/n", P );
Return 0;
}
Function Name: strtol
Function: converts a string to a long integer.
Usage: Long strtol (char * STR, char ** endptr, int base );
Program example:
# I nclude <stdlib. h>
# I nclude <stdio. h>
Int main (void)
{
Char * string = "87654321", * endptr;
Long lnumber;
/* Strtol converts string to long integer */
Lnumber = strtol (string, & endptr, 10 );
Printf ("string = % s long = % LD/N", String, lnumber );
Return 0;
}
Function Name: strupr
Function: converts lowercase letters in a string to uppercase letters.
Usage: char * strupr (char * Str );
Program example:
# I nclude <stdio. h>
# I nclude <string. h>
Int main (void)
{
Char * string = "abcdefghijklmnopqrstuvwxyz", * PTR;
/* Converts string to upper case characters */
PTR = strupr (string); printf ("% s/n", PTR );
Return 0;
}
Function Name: swab
Function: swap byte
Usage: void swab (char * From, char * To, int nbytes );
Program example:
# I nclude <stdlib. h>
# I nclude <stdio. h>
# I nclude <string. h>
Char source [15] = "rfna koblrna D"; char target [15];
Int main (void ){
Swab (source, target, strlen (source ));
Printf ("this is target: % s/n", target );
Return 0;
}

From: http://www.yuanma.org/data/2006/1029/article_1738.htm

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.