[Add to favorites] C string processing functions

Source: Internet
Author: User
Tags strtok

Function Name: strcpy
Function: copy one string to another.
Usage: char * stpcpy (char * Destin, char * Source );
Program Example:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char string [10];
Char * str1 = "abcdefghi ";
Stpcpy (string, str1 );
Printf ("% Sn", string );
Return 0;
}



Function Name: strcat
Function: String concatenation Function
Usage: char * strcat (char * Destin, char * Source );
Program example:
# Include <string. h>
# Include <stdio. h>
Int main (void)
{
Char destination [25];
Char * blank = "", * c = "C ++", * Borland = "Borland ";
Strcpy (destination, Borland );
Strcat (destination, blank );
Strcat (destination, C );
Printf ("% Sn", destination );
Return 0;
}



Function Name: strchr
Function: searches for the first matching of a given character in a string.
Usage: char * strchr (char * STR, char C );
Program example:
# Include <string. h>
# Include <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: % DN", C, PTR-string );
Else
Printf ("the character was not foundn ");
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:
# Include <string. h>
# Include <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 1N ");
Else
Printf ("Buffer 2 is less than buffer 1N ");
PTR = strcmp (buf2, buf3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 3N ");
Else
Printf ("Buffer 2 is less than buffer 3N ");
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:
# Include <string. h>
# Include <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 1N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1N ");
Return 0;
}



Function Name: strcpy
Function: Copy strings
Usage: char * strcpy (char * str1, char * str2 );
Program example:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char string [10];
Char * str1 = "abcdefghi ";
Strcpy (string, str1 );
Printf ("% Sn", 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:
# Include <stdio. h>
# Include <string. h>
# Include <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 % DN", length );
Return 0;
}



Function Name: strdup
Function: copy the string to the new position.
Usage: char * strdup (char * Str );
Program example:
# Include <stdio. h>
# Include <string. h>
# Include <alloc. h>
Int main (void)
{
Char * dup_str, * string = "ABCDE ";
Dup_str = strdup (string );
Printf ("% Sn", dup_str );
Free (dup_str );
Return 0;
}



Function Name: stricmp
Function: Compares two strings in upper and lower case insensitive mode.
Usage: int stricmp (char * str1, char * str2 );
Program example:
# Include <string. h>
# Include <stdio. h>
Int main (void)
{
Char * buf1 = "BBB", * buf2 = "BBB ";
Int PTR;
PTR = stricmp (buf2, buf1 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1N ");
Return 0;
}


Function Name: strerror
Function: returns a pointer to the error message string.
Usage: char * strerror (INT errnum );
Program example:
# Include <stdio. h>
# Include <errno. h>
Int main (void)
{
Char * buffer;
Buffer = strerror (errno );
Printf ("error: % Sn", buffer );
Return 0;
}



Function Name: strcmpi
Function: compares a string with another string, regardless of case.
Usage: int strcmpi (char * str1, char * str2 );
Program example:
# Include <string. h>
# Include <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 1N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1N ");
Return 0;
}



Function Name: strncmp
Function: String comparison
Usage: int strncmp (char * str1, char * str2, int maxlen );
Program example:
# Include <string. h>
# Include <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 1N ");
Else
Printf ("Buffer 2 is less than buffer 1N ");
PTR = strncmp (buf2, buf3, 3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 3N ");
Else
Printf ("Buffer 2 is less than buffer 3N ");
Return (0 );
}


Function Name: strncmpi
Function: compares a part of a string with a part of another string, regardless of case.
Usage: int strncmpi (char * str1, char * str2 );
Program example:
# Include <string. h>
# Include <stdio. h>
Int main (void)
{
Char * buf1 = "bbbccc", * buf2 = "bbbccc ";
Int PTR;
PTR = strncmpi (buf2, buf1, 3 );
If (PTR> 0)
Printf ("Buffer 2 is greater than buffer 1N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1N ");
Return 0;
}


Function Name: strncpy
Function: Copy strings
Usage: char * strncpy (char * Destin, char * Source, int maxlen );
Program example:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char string [10];
Char * str1 = "abcdefghi ";
Strncpy (string, str1, 3 );
String [3] = '';
Printf ("% Sn", string );
Return 0;
}


Function Name: strnicmp
Function: Compares two strings case-insensitive.
Usage: int strnicmp (char * str1, char * str2, unsigned maxlen );
Program example:
# Include <string. h>
# Include <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 1N ");
If (PTR <0)
Printf ("Buffer 2 is less than buffer 1N ");
If (PTR = 0)
Printf ("Buffer 2 equals buffer 1N ");
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:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char * string = "abcdefghijklmnopqrstuvwxyz ";
Char letter = 'X ';
Printf ("string before strnset: % Sn", string );
Strnset (string, letter, 13 );
Printf ("string after strnset: % Sn", 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:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char * string1 = "abcdefghijklmnopqrstuvwxyz ";
Char * string2 = "onm ";
Char * PTR;
PTR = strpbrk (string1, string2 );
If (PTR)
Printf ("strpbrk found first character: % CN", * PTR );
Else
Printf ("strpbrk didn't find character in setn ");
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:
# Include <string. h>
# Include <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: % DN", C, PTR-string );
Else
Printf ("the character was not foundn ");
Return 0;
}



Function Name: strrev
Power: String reversal
Usage: char * strrev (char * Str );
Program example:
# Include <string. h>
# Include <stdio. h>
Int main (void)
{
Char * Forward = "string ";
Printf ("before strrev (): % Sn", forward );
Strrev (forward );
Printf ("after strrev (): % Sn", 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:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char string [10] = "123456789 ";
Char symbol = 'C ';
Printf ("before strset (): % Sn", string );
Strset (string, symbol );
Printf ("after strset (): % Sn", 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:
# Include <stdio. h>
# Include <string. h>
# Include <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 % DN", 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:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char * str1 = "Borland International", * str2 = "nation", * PTR;
PTR = strstr (str1, str2 );
Printf ("the substring is: % Sn", PTR );
Return 0;
}


Function Name: strtodd
Function: converts a string to a double value.
Usage: Double strtodd (char * STR, char ** endptr );
Program example:
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
{
Char input [80], * endptr;
Double value;
Printf ("enter a floating point number :");
Gets (input );
Value = strtodd (input, & endptr );
Printf ("the string is % s the number is % LFN", input, value );
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:
# Include <string. h>
# Include <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 ("% Sn", 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 ("% Sn", 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:
# Include <stdlib. h>
# Include <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 = % ldn", String, lnumber );
Return 0;
}

Function Name: strupr
Function: converts lowercase letters in a string to uppercase letters.
Usage: char * strupr (char * Str );
Program example:
# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Char * string = "abcdefghijklmnopqrstuvwxyz", * PTR;
/* Converts string to upper case characters */
PTR = strupr (string );
Printf ("% Sn", PTR );
Return 0;
}



Function Name: swab
Function: swap byte
Usage: void swab (char * From, char * To, int nbytes );
Program example:
# Include <stdlib. h>
# Include <stdio. h>
# Include <string. h>
Char source [15] = "rfna koblrna D ";
Char target [15];
Int main (void)
{
Swab (source, target, strlen (source ));
Printf ("this is target: % Sn", target );
Return 0;
}

PS: isalpha () is a character function, not a string function,

Isalpha

Prototype: extern int isalpha (int c );

Usage: # include <ctype. h>

Function: determines whether the character C is an English letter.

(When C is an English letter A-Z or A-Z, return a non-zero value, otherwise return zero.

Example:

// Isalpha. c

# Include <syslib. h>
# Include <ctype. h>
# Include <stdio. h>

main ()
{< br> int C;
clrscr (); // clear screen
printf ("press a key");
for (;)
{< br> C = getchar ();
clrscr ();
printf ("% C: % s letter", C, isalpha (c )? "Is": "not");
}< br> return 0; // just to avoid warnings by compiler
}

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.