About character arrays in C, character pointers, and 22 conversions and sorting of string types in C + +

Source: Internet
Author: User

//practise.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<string.h>#include<string>#include<algorithm>#include<iostream>using namespacestd;intMain () {/*string to character array, using strncpy_s (), than strncpy () security*/    stringstr ="ABC xyz"; Charstrarray[ One] = {0};    strncpy_s (Strarray, Str.c_str (), str.length ()); printf ("Strarray:%s\n", Strarray); cout<< Strarray <<Endl; /*string to character pointer, call C_STR () directly*/    Const Char* Strpointer =""; Strpointer=Str.c_str (); printf ("Strpointer:%s\n", Strpointer); cout<< Strpointer <<Endl; /*the character pointer is converted into a string, directly assigned to the value*/    Char* StrPointer1 ="bat Hzx"; stringSTR1 =StrPointer1; printf ("str1:%s\n", Str1.c_str ()); /*the character array is converted to a string, directly assigned to a value*/    CharStrarray1[] ="hzx Bat"; stringSTR2 =StrArray1; printf ("str2:%s\n", Str2.c_str ()); /*the character pointer is converted into a character array, strncpy_s ()*/    Char* StrPointer2 ="Cheng du"; //int strLen = StrLen (StrPointer2); //char Strarray2[strlen] = {0};//error C2057: The constant expression should be entered    Charstrarray2[ One] = {0};    strncpy_s (StrArray2, StrPointer2, strlen (StrPointer2)); printf ("strArray2:%s\n", StrArray2); /*character array into character pointer, direct assignment*/    Charstrarray3[ -] ="Beijing China"; Char*StrPointer3; StrPointer3=StrArray3; printf ("StrPointer3:%s\n", StrPointer3); /*how the character array compares size, using the strcmp () function*/    Charstrarray4[8] ="ABC"; Charstrarray5[8] ="ABX"; Charstrarray6[8] ="ABC"; printf ("StrArray4:%d strArray5:%d strArray6:%d\n", StrArray4, StrArray5, StrArray6); printf ("Is big:%d\n", strcmp (StrArray4, strArray5)); printf ("Is big:%d\n", strcmp (StrArray4, StrArray6)); printf ("Is big:%d\n", strcmp (StrArray5, StrArray6)); /*How the character pointer compares size, using the strcmp () function*/    Char* StrArray7 ="ABC"; Char* StrArray8 ="Aby"; Char* StrArray9 ="ABC"; printf ("StrArray7:%d StrArray8:%d strArray9:%d\n", StrArray7, StrArray8, StrArray9); printf ("Is big:%d\n", strcmp (StrArray7, StrArray8)); printf ("Is big:%d\n", strcmp (StrArray7, StrArray9)); printf ("Is big:%d\n", strcmp (StrArray8, StrArray9)); /*how character pointers and character arrays compare size, using the strcmp () function*/    Char* StrArray10 ="ABC"; Charstrarray11[5] ="Abz"; Char* StrArray12 ="ABC"; printf ("StrArray10:%d StrArray11:%d strArray12:%d\n", StrArray10, StrArray11, STRARRAY12); printf ("Is big:%d\n", strcmp (StrArray10, StrArray11)); printf ("Is big:%d\n", strcmp (StrArray10, strArray12)); printf ("Is big:%d\n", strcmp (StrArray11, strArray12)); /*How the string compares size, using strcmp () +c_str () combination or direct compare ()*/    stringStr13 ="ABC"; stringStr14 ="ABX"; stringStr15 ="ABC"; printf ("Str13:%d str14:%d str15:%d\n", Str13, Str14, STR15); printf ("Is big:%d\n", strcmp (Str13.c_str (), Str14.c_str ())); printf ("Is big:%d\n", strcmp (Str13.c_str (), Str15.c_str ())); printf ("Is big:%d\n", strcmp (Str14.c_str (), Str15.c_str ())); cout<< Str13.compare (str14) <<Endl; cout<< Str13.compare (STR15) <<Endl; cout<< Str14.compare (STR15) <<Endl; return 0;}

About character arrays in C, character pointers, and 22 conversions and sorting of string types in C + +

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.