Summary of string correlation functions

Source: Internet
Author: User

Char class:

1.strcpy (S1,S2); Copy the String S2 as the starting address into S2.

2.strncpy (s1,s2,k); copies the first k of the string S2 as the starting address into S1.

Char s1[],s2[];cin>>S2; int k=4; strncpy (s1,s2,k); S1[k]='n'; cout <<s1<<endl;

3.strstr (S1,S2); finds S2 in S1, returns the first occurrence if found, otherwise returns NULL.

Char s1[]={"ABC"},s2[]={"iiiabciii"}; Char* pos=strstr (S2,S1); if (pos!=null) printf ("%s\n", POS); // Output: ABCIII

4.STRCMP (S1,S2); Compares the size of S1 and S2 by dictionary order. The equivalent return 0;S1>S2 returns 1;S1<S2 returns-1.

5.STRNCMP (s1,s2,n); the STRNCMP function specifies the 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.

Char s1[]={"ABCDD"}; Char s2[]={"ABC"};p rintf ("%d\n", STRNCMP (S1,S2,3));

String class:

1. STRING.C_STR ();

The const char* type is returned;

string s;cin>>s; Const Char* str=s.c_str ();p UTS (str);

Summary of string correlation functions

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.