C-language string manipulation

Source: Internet
Author: User

Tag: equals byte return hello log hang greater than strlen OID

C-language string manipulation function 1.strlen

Strlen is used to find the length of a C-style string, and the function prototype is

#include <string.h>       size_t strlen (constchar *s);

The return value is the length of the string, and when it encounters a ' + ', the string is considered to be ending, and '/' is not counted into the length.

#include <stdio.h><string.h>int main (void) {      Char"hello,world";     int count = strlen (str);    printf ("%d\n", Count);     return 0 ;}

Results tested: 11

The test results are as expected, and the following 13 ways to implement your own strlen function.

size_t My_strlen1 (Const Char*str) {    intCount=0; ASSERT (str);//while (str[count++]);//return count-1;     while(*str++) Count++; returncount;} size_t My_strlen2 (Const Char*str)    {assert (str); if(*str = =' /')return 0; Else returnMy_strlen2 (++STR) +1;} size_t My_strlen3 (Const Char*str) {    Char*headstr =str;    ASSERT (str);  while(*++str); returnSTR-headstr;}

The first one uses the traversal string once, when the character is not ' count++ ';

The second use of recursion

The third one uses the pointer's haircut, subtracting the pointer to the first character with the pointer to the last character.

2.strnlen

The prototype of Strnlen is

  #include <string.h>  size_t strnlen (constchar *s, size_t maxlen);

Strnlen function is equivalent to a counter, the function from the beginning of the number of S, until the meeting ' \ ' or the counter reaches the value of Maxlne, stop counting.

Its use is mainly in the network graphics, the word stream is generally no ' strlen ', so, using the general will have problems, all introduced strnlen.

3.strcpy

The prototype of strcpy is

       #include <string.h>       char *strcpy (charconstchar *src);

The function of strcpy is to copy the string starting with SRC (including ' s ') to the address space with Dest as the starting point, and the return value as a pointer to Dest.

#include <stdio.h><string.h>int  main () {    char dest[  - ];     Char " Hello,world " ;    strcpy (DEST,SRC);    printf ("%s\n", dest);     return 0 ;}

Test Result: Hello,world

Meet expectations

Realize your strcpy.

#include <stdio.h><assert.h>char *my_strcpy (char *dest,const  Char *src) {    assert (dest);    ASSERT (SRC);      while (*dest++ = *src++);     return dest;}
4.strncpy

The prototype of strncpy is

#include <string.h>    char *strncpy (charconstChar *src, size_t N);

The function is the same as the string copy function, and unlike the strcpy, there is one more parameter n. The function is to copy the first n characters from the string pointed to by SRC to dest.

5.strcmp
#include <string.h>       int strcmp (constcharconstChar *s2);

strcmp is used to compare the size of strings S1 and S2, compared to dictionary comparisons, and to compare ASCII values of characters, such as "Erfas" and "Eradfasa";

Compare the first two characters to equal, the third character ' F ' is greater than ' a ', so the string is large.

Returns a positive number when S1 is greater than S2;

When S1 equals S2, returns 0;

When S1 is less than S2, a negative number is returned;

#include <string>#include<stdio.h>intMain () {Char*S1 ="Hello wolrd"; Char*S2 =" Change World"; intCount =strcmp (S1,S2); printf ("%d\n", Count); return 0;}

This code has a result of 1 under VS2010 and a test result of 5 under GCC

The C language standard only specifies that when S1 is greater than S2, the return value is positive, VS returns 1, and GCC returns the difference of the ASCII value, so for program

Portability, do not write

1

Such code, it is recommended to write

strcmp (S1,S2) > 0

Below we try to achieve our own strcmp

#include <stdio.h>#include<assert.h>intMY_STRCMP (Const Char*S1,Const Char*S2)    {assert (S1);    ASSERT (S2);  while((*S1) = = (*S2)) {        if((*s1) = =' /')            return 0; S1++; S2++; }    if((*S1)-(*S2) >0)return 1; Else return-1;}
6.strncmp

The prototype of STRNCMP is

#include <string.h>     int strncmp (constcharconst  Char *s2, size_t N);

The function is basically similar to strcmp, but only the first n characters of two strings are compared.

7.strstr

The prototype of STRSTR is

#include <string.h>       char *strstr (constcharconstChar *needle);

Determines whether needle is a substring of haystack, returns the address that needle first appears in haystack, or returns null

#include <string.h>#include<stdio.h>intMain () {Char*S1 ="Hello,world"; Char*S2 ="llo,w"; Char*S3 =strstr (S1,S2); printf ("%s\n", S3); return 0;}

The test results are:

Llo,world

Results match expectations

8.memset

The prototype of Memset is

#include <string.h>       void *memset (voidint C, size_t N);

The function of memset is to place the first n bytes in the memory pointed by S into the ASCII value of character C.

#include <string.h>#include<stdio.h>intMain () {Chars[ -]; memset ((Char*) S,'C',Ten); s[Ten] =' /'; printf ("%s\n", s); return 0;}

Test Result: CCCCCCCCCC

9.memcpy

memcpy

#include <string.h>       void *memcpy (voidconstvoid *src, size_t n);

Copy src data N to dest

 #include <string . H> #include  <stdio.h>int   main () { char  src[100 ] = "  Span style= "COLOR: #800000" >hello,world   "     ;  char  dest[100     ];    memcpy (DEST,SRC,  11    " %s\n   "    ,dest);  return  0  ;}  

Test Result: Hello,world

Meet expectations

MEMCPY does not consider memory overlap

10.memmove

The prototype of Memmove is

#include <string.h>       void *memmove (voidconstvoid *src, size_ T n);

Memmove is similar to memcpy, but can handle memory overlap issues.

  

  

  

  

C-language string manipulation

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.