C-Style String Operation

Here introduces C-style 'string' (mostly char[]) operation.

It's important that with classes and many more useful and Memory Safe functions, string shall not be processed with these functions.

  1. strcpy(dst,source),return dstcopy source to destination, returning the address of destination. be cautious that it can cause memory leak. example:

    char b[5]={0};

    char a[]="abcdefghijklmn";

    cout << (int)strcpy(b,a);you'll get an address.

    cout << ' ';

    for(int i = 0;i<=15;i++)cout<<b[i];


    6422204          abcdefghijklmn#
    #:memory leak^
  2. strncpy(dst,src,n),return dstcopy n characters at most from source to destination. returning the address of destination. (better than strcpy to avoid memory leak.)

  3. strcat(dst,src),return dstadd src to the end of dst. Returning the address of dst.

  4. strncat(dst,src,n),return dst analogise with 2.

  5. strlen(s),return intlength of a string(to the \0, not the size of a list).

  6. strcmp(s1,s2),return intreturn (s1-s2).

  7. strncmp(s1,s2,n),return int,anal. with 2.

  8. strchr(s,ch),return addrreturn the address of the first ch in s.


    char a[]="abcdefghijklmn";

    cout<<strchr(a,'f'); returns: 6422278


    char a[]="abcdefghijklmn";

    cout<<(int)strchr(a,'q'); returns: 0

  9. strrchr(s,ch),return addrreturn the address of the last ch in s.

  10. strstr(s1,s2),return addranal with 7,but finding s2.

Ads by Google

Read our privacy policy on how these personalized advertisements are delivered to you.

For your reading experience, we provide full-text RSS feeds. Although math formulas cannot be displayed well, the interface can be adjusted as you like and there are no ads.