C Language Study Notes (9)

Source: Internet
Author: User

In this section, I want to introduce the common functions used to operate strings in C language.

The prototype of the string function in C Language resides in the <string. h> header file. Therefore, we need to introduce this header file when processing the string.

However, I don't want to simply introduce the usage of each function. If so, I can look at the API. I want to implement it myself when introducing each function.

1. strcpy Function

String Copy. Let's take a look at the usage of this function.

 
# Include<Stdio. h># Include<String. h>IntMain (){Char* Str1 ="ABCD";CharStr2 [5];Strcpy (str2, str1); printf ("% S", Str2 );Return0 ;}

AboveCode, We note that the str2 string length should be 5, that is, strlen ("ABCD") plus '\ 0 '. In the above Code, we copied the str1 string to str2.

Let's also look at the differences between the two codes:

 
# Include<Stdio. h># Include<String. h>IntMain (){CharStr1 [5] ="ABCD";CharStr2 [5];Char* Str3 = str1; strcpy (str2, str1); str1 [1] ='T'; Printf ("Str2: % s \ n", Str2); printf ("Str3: % s", Str3 );Return0 ;}

The comparison results show their differences. Here, we may look back at Java/C #'s deep replication and shallow replication.

When we modify the first code as follows, we will find an error:

 
# Include<Stdio. h># Include<String. h>IntMain (){CharStr1 [5] ="ABCD";CharStr2 [3]; strcpy (str2, str1); str1 [1] ='T'; Printf ("Str2: % s \ n", Str2 );Return0 ;}

The cause of the error is that str2 is not enough to hold the str1 string. That is to say, strcpy does not have enough control, so we can use the strncpy method to implement it:

# Include<Stdio. h># Include<String. h>IntMain (){CharStr1 [5] ="ABCD";CharStr2 [3]; strncpy (str2, str1,Sizeof(Str2)-1); str2 [Sizeof(Str2)-1] ='\ 0'; Printf ("Str2: % s \ n", Str2 );Return0 ;}

This method is not much said, that is, a length control is added. Next we will try to implement a simple strcpy function.

  # include   
    
    #include   
    
     char  * mycopy ( char  *,  const char  *);  int  main () { char  str1 [5] = " ABCD ";  char  str2 [5]; mycopy (str2, str1 ); printf ( "% s" , str2);  return  0 ;}< span style =" color: Blue "> char  * mycopy ( char  * strdest,  const char  * strsource) { char  * temp = strdest;  while  (* temp ++ = * strsource ++ );  return  strdest ;}

Similarly, we can also write strncpy code:

 
Char* Mycopy (Char* Strdest,Const char* Strsource,IntSize ){Char* Temp = strdest;IntI = 0;While(I <size) {* temp ++ = * strsource ++; I ++;} * temp ='\ 0';ReturnStrdest ;}

2. Let's talk about the string.

In fact, I have never understood the problem of modifying a character in a string. I have been troubled by the following problems:

In factWhether the string can be modified depends on whether the string is in the constant area..

3. strlen Function

This is easy to implement:

Size_t mylength (Char* Str ){IntCount = 0;While(* STR ++) {count ++ ;}Return(Size_t) count ;}

However, if this is used for writing, an addition operation is required each time when the pointer is moved forward, which may affect the efficiency. We can write it like this:

 
Size_t mylength (Char* Str ){Char* Temp = STR;While(* Temp ++ );Return(Size_t) (temp-str-1 );}

4. strcmp Functions

String comparison functions:

IntMycompare (Const char* Str1,Const char* Str2 ){While(* Str1 = * str2 ){If(* Str1 ='\ 0'){Return0;} str1 ++; str2 ++ ;}Return* Str1> * str2? 1:-1 ;}

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.