C Language--string

Source: Internet
Author: User
Tags strcmp strtok

1. String

Representations: Char ch[5]={' A ', ' B ', ' C ', ' d ', ' e '};

Note the point:

Enclosed in double quotes.

Each character occupies a single byte

End of string as ' \ '

int main (int argc, const char * argv[]) {    char str[100]= "Hahahaha\0wewewe\0helloworld";    printf ("%s\n", str);    printf ("%s\n", &str[9]);    printf ("%s\n", &str[16]);    return 0;}

1.1 String length

Note the point:

String valid length does not contain '/'

sizeof is an operator that takes a variable or constant to take up the amount of memory space

Strlen is a function that asks for the number of valid characters in a string

int Mystrlen (const char * src) {    int i=0;    while (src[i]!= ') {        i++;    }    return i;} int main (int argc, const char *argv[]) {    char str[100]= "Hello World";    printf ("%d \ n", Mystrlen (str));//Call function to find the length    of printf ("%ld \ n", sizeof (str));    printf ("%ld \ n", sizeof ("Hello World"));    printf ("str = =%ld \ n", strlen (str));    return 0;}

1.2 string copy (copy)

Function:

strcpy

Char *strcpy (char *, const char *);

strncpy

Char *strncpy (char *, const char *, size_t);

Note the point:

S1 has enough memory space to hold S2 string

strncpy Select the number of bytes copied, does not necessarily include ';

Custom copy function char *mystrcpy (char *dst, const char *src) {    int i =0;    while (Src[i]) {        dst[i]=src[i];        i++;    }    dst[i]= ' + ';    return DST;} int main (int argc, const char *argv[]) {    char s1[100];    Char s2[50]= "Hello world\n";    Char *str = strcpy (s1, S2);    printf ("%s\n", strcpy (S1, S2));    printf ("%s\n", str);    printf ("%s\n", mystrcpy (S1, S2));    strncpy (S1, S2, ten);    s1[10]= ' + ';    printf ("%s\n", S1);        return 0;}

1.3 string concatenation

Function:

Strcat

Char *strcat (char *, const char *);

Strncat:

Char *strncat (char *, const char *, size_t);

Strncat a new string after stitching will add a '/';

Custom stitching function Char *mystrcat (char *s1, const char *s2) {    int i=0,j;    while (S1[i]) {        i++;    }   Loop stitching operation for    (j=0; s2[j]!= '; j + +) {        s1[i+j]=s2[j];    }    s1[i+j]= ' + ';    return s1;} int main (int argc, const char *argv[]) {    char s1[100]= "Hello World";    Char s2[50]= "Wearefriend";        printf ("%s\n", strcat (S1, S2));    printf ("%s\n", Mystrcat (S1, S2));    printf ("%s\n", Strncat (S1, S2, 5));        return 0;}

1.4 String comparison function

Function:

strcmp

int strcmp (const char *, const char *);

strncmp

int strncmp (const char *, const char *, size_t);

S1 > S2 Returns a positive number

S1 = = S2 return 0

S1 < S2 returns a negative number

Custom comparison function int mystrcmp (const char* s1, const char *s2) {    int i;    The loop is judged at the end of the string for    (i=0; (s1[i]!= ') && (s2[i]!= '); i++) {        if (S1[i]==s2[i]) {            continue;        }        Else        {break            ;            return  s1[i]-s2[i];        }    }    Returns a comparison number    return s1[i]-s2[i];} int main (int argc, const char *argv[]) {    char *str1 = "Borld";    Char *str2 = "Zorld";        printf ("%d\n", mystrcmp (STR1, str2));    printf ("%d\n", strncmp (str1, STR2, 7));    Char s1[30]= "Hello World";        return 0;}

1.5 String Lookup

Function:

STRCHR:

Char *STRCHR (const char *, int);

STRRCHR:

Char *STRRCHR (const char *, int);

Char *mystrchr (const char *S1, char ch) {    int i=0;    while (S1[i]) {        if (s1[i]==ch) {break            ;        }        i++;    }    Return (char *) &s1[i];} int main (int argc, const char *argv[]) {    char str[100]= "Hello world!";        Char *pstr = MYSTRCHR (str, ' l ');    printf ("%s\n", pstr);    printf ("%s\n", pstr);    return  0;}

1.6 Finding a string

Function:

Strstr

Char *strstr (const char *, const char *);

int main (int argc, const char *argv[]) {    char str1[100]= "HelloWorld HelloWorld";    printf ("%p\n", &str1[strlen (STR1))),//    char *pstr = strstr (str1, "World");//    printf ("%s\n", pstr)    ; char *p = str1;    while (P=strstr (p, "world")) {        if (p) {            printf ("%s\n", p);            P+=sizeof ("World")-1;        }    }    return 0;}

1.7 String Segmentation

Function:

Strtok

Char *strtok (char *, const char *);

int main (int argc, const char *argv[]) {    char str[100]= "Hello World:good-bye:://we^are". Friend () ok///";//    char *p=strtok (str,": ");//    printf ("%s\n ", p);//While    ((P=strtok (NULL,":/^. () -"))) {//        printf ("%s\n ", p);/    }    char *p = str;    while (P=strtok (P, ":/^. () -")")    {        printf ("%s\n", p);        P=null;    }    p = strtok (NULL, ":");//    printf ("%s\n", p);    return 0;}

eg. dividing a string by a space to get the number of words (without using system functions)

int main (int argc, const char *argv[]) {    char str[200];    scanf ("%[^\n]", str);    int i=0;    int flag=1;//flag bit    int cnt=0;//statistic number while    (str[i]!= ') {if (        str[i]== ') {            if (!flag) {                flag = 1;
   }        }        else        {            if (flag) {                cnt++;                Flag =0;            }        }        i++;    }    printf ("%d\n", CNT);    return 0;}

C Language--string

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.