String manipulation functions common in the "grooming" C language

Source: Internet
Author: User

The function of string manipulation is often used in the development of requirements, and the functions are summarized as follows:

Function Name: strcpy

Function: Copy one string to another
Usage: char *strcpy(char *destin, char *source);

Examples are as follows:

<span style= "FONT-SIZE:24PX;" > #include <stdio.h> #include <string.h>int main () {    char desstr[10];    Char *srcstr = "ABCDEFG";    strcpy (DESSTR,SRCSTR);    printf ("%s\n", Desstr);} </span>

function Name: strcat
Function: string concatenation function
Usage: char *strcat (char *destin, char *source);
program Example:
<pre name= "code" class= "CPP" > #include <stdio.h> #include <string.h>int main () {    char temstr [50] = {0};    Char *desstr = "123456";    Char *srcstr= "789";    char *temp = NULL;    strcpy (TEMSTR,DESSTR);    strcat (TEMSTR,SRCSTR);    temp = STRCHR (temstr, ' 7 ');    printf ("%s", temp);}


function Name: STRCHR
function: Finds the first match of a given character in a string
usage: char *strchr (char *str, char c);

Description: Returns a pointer to the position of the first occurrence of C, the address returned is the first pointer to the same character as Val at the beginning of the lookup string pointer, and returns NULL if there is no C in S.

Program examples

#include <string.h> #include <stdio.h>int main () {    char temstr [] = {0};    Char *desstr = "123456";    Char *srcstr= "789";    strcpy (TEMSTR,DESSTR);    strcat (TEMSTR,SRCSTR);    printf ("%s", TEMSTR);}
function Name: strcmp
function: string comparison
usage: int strcmp (char *str1, char *str2);
See ASIC code, STR1>STR2, return value > 0, two strings equal, return 0

/* When S1<S2, returns a negative number when s1=s2, return value = 0 when s1>s2, returns positive */#include <stdio.h> #include <string.h>int main () {    Char string[20];    Char str[3][20];    int i;    for (i=0;i<3;i++)        gets (Str[i]);    if (strcmp (str[0],str[1]) >0)        strcpy (string,str[0]);    else        strcpy (string,str[1]);    if (strcmp (str[2],string) >0)        strcpy (string,str[2]);    printf ("\nthe largest string is%s\n", string);    Return0;}

function Name: strcspn
function: Finds the segment of the first given character set content in a string
usage: int strcspn (char *str1, char *str2);

Function Description: strcspn () computes consecutive characters from the beginning of the argument s string, which are not in the string that the parameter reject refers to. Simply put, if STRCSPN () returns a value of n, it means that the string s starts with n characters without a character within the string reject. Return value: Returns the number of characters in a string that is not preceded by a string reject
Program Example:

#include <stdio.h> #include <string.h>int main () {    char *desstr = "Holle, hi 123";    Char *srcstr= ",";    int relust = 0;    Relust = strcspn (DESSTR,SRCSTR);    printf ("%d", relust);}

function Name: StrDup
function: Copy the string to the new location
usage: char *strdup (char *str);

Function: Copy the string to the new location StrDup () internally calls malloc () to allocate memory for the variable, without using the returned string, freeing the appropriate memory space with free (), or it will cause a memory leak.
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);     

function Name: stricmp
function: Comparison of two strings in case insensitive manner
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);

A pointer to the error message (that is, the description string for the error).
program Example:

#include <stdio.h> #include <string.h> #include <errno.h> #include <stdlib.h>int main (void) { File*fp;externinterrno;char*message;if (null== (Fp=fopen ("/DEV/DSP", "r+")) {printf ("errno=%d\n", errno); message= Strerror (errno);p rintf ("mesg:%s\n", message); Exit (0);} /* Output: Error=2mesg:no such file or direcory*/


function Name: strncmp
function: string comparison
usage: int strncmp (char *str1, char *str2, int maxlen);
Program Example:

The/*STRNCMP function is a specified comparison of size characters. That is, if the string S1 is the same as the first size character of S2, the function returns a value of 0. This function functions to compare the first maxlen characters of a string str1 and str2. If the first maxlen bytes are exactly equal, the return value is = 0, and in the previous MaxLen byte comparison, if Str1[n] and str2[n are present, then the first n bits of str1 and str2 are compared sequentially, and the I (I<n) is the first different bit of the two strings, then the return (str1 [I]-str2[i]). */#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 ("Buffer2 is greater than buffer1\n"); ElseIf (ptr<0) printf (" Buffer2 is less than buffer1\n ");p tr=strncmp (buf2,buf3,3), if (ptr>0) printf (" Buffer2 is greater than buffer3\n "); ElseIf (ptr<0) printf ("Buffer2 is less than buffer3\n"); return (0);}

function Name: strncpy
function: string copy
Usage: char *strncpy (char *destin, char *source, int maxlen);

can be compared with strcpy method;
program Example:

#include <stdio.h> #include <string.h> int main (void) {    char string[10];    Char *str1 = "Abcdefghi";    strncpy (String, str1, 3); /*string must be guaranteed to have enough space to store the copied string */   string[3] = ';    printf ("%sn", string);    

Function Name: Strnset
Usage: char*strnset (Char*str,char ch,unsigned N);
Function: Sets the first n characters in a string to the specified character ch

program Example:

#include <stdio.h> #include <string.h>int main (void) {char string[55]= "abcdefghijklmnopqrstuvwxyz"; char letter= ' x ';p rintf ("stringbeforestrnset:%s\n", String), Strnset (string,letter,13);p rintf ("stringafterstrnset:%s\n ", string); return 0;}

function Name: strpbrk
function: Finds 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"; C1/>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");    

function Name: Strstr
function: Finds the first occurrence of a specified string in a string
usage: char *strstr (char *str1, char *str2);
Program Example:
#include <stdio.h> #include <string.h> int main (void) {    char *str1 = "Borland International", *STR2 = "nat Ion ", *ptr;    ptr = Strstr (str1, str2);    printf ("The substring is:%sn", PTR);    

Function Name: strtod
Function: Converts a string to a double type value
Usage: Double strtod (char *str, char **endptr);
program Example:


/*strtod () scans the parameter nptr string, skips the preceding space character until a number or sign is encountered, and then ends the conversion until a non-digit or string ends (' + '), and returns the result. If endptr is not null, the character pointer in nptr that terminates with an unqualified condition is returned by ENDPTR. The parameter nptr string can contain a positive sign, a decimal point, or E (e) to represent the exponential portion. such as 123.456 or 123e-2. */#include <stdlib.h> #include <stdio.h>void main () {    char *endptr;    Char a[] = "12345.6789";    Char b[] = "1234.567qwer";    Char c[] = " -232.23e4";    printf ("a=%lf\n", Strtod (A,null));    printf ("b=%lf\n", Strtod (B,&endptr));    printf ("endptr=%s\n", endptr);    printf ("c=%lf\n", Strtod (C,null));} /* Execute: 1234a=12345.678900b=1234.567000endptr=qwerc=-2322300.000000*/
function Name: Strtol
function: Converts a string to a long integer
usage: Long strtol (char *str, char **endptr, int base);
Program Examples

#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, ten);    printf ("string =%s  long =%ldn", string, lnumber);    return 0; }

function Name: STRUPR
function: Converts lowercase letters in strings 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);    
function Name: Swab
function: Swap bytes
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; }


Of course there are many welcome additions!














Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

String manipulation functions common in the "grooming" C language

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.