Character Array Function, connected to the strcat replication function strcpy comparison function strcmp length function strlen, strcatstrcmp

Source: Internet
Author: User
Tags float double

Character Array Function, connected to the strcat replication function strcpy comparison function strcmp length function strlen, strcatstrcmp

Before we learned the data type, there was a char type, which allowed us to put a character inside.

Char variable1 = 'O ';

Char variable2 = 'K ';

# Include <iostream> using namespace std; int main () {// assign values to character arrays in C ++, ''single quotes cannot be empty char talk [10] = {'I', '', 'A', 'M','', 'h', 'A ', 'P', 'P', 'y'}; for (int I = 0; I <10; I ++) {cout <talk [I];} cout <endl ;}

Outputs a diamond image using a two-dimensional array of characters.

# Include <iostream> using namespace std; int main () {// assign values to character arrays in C ++, ''single quotes cannot be empty char talk [5] [5] = {{'','','*'},{'','*','', '','*'},{'*','','','','*'},{'','*','', '*'}, {'','', '*'}; for (int I = 0; I <5; I ++) {for (int j = 0; j <5; j ++) cout <talk [I] [j]; cout <endl ;}cout <endl ;}
What is the length of this character array?
Char talk [10] = {'I', '', 'A', 'M','', 'h', 'A', 'P ', 'P', 'y '};
String I am happy
String talk = "I am happy ";
The meaning of the two is the same, but the length is different. C ++ automatically adds a '\ 0' after character y, which indicates the end of the string.
String is different from the basic data type of the int float double boolean char we learned earlier. It can be used directly in the c ++ program.
String cannot be directly used in c ++.
# Include <iostream> # include <string> using namespace std; int main () {int a = 1; cout <a; // What is the length of s? 11 c ++ automatically adds a '\ 0' // when using the string. When the c ++ system outputs the string, it detects' \ 0 ', end string s = "I am happy"; // What is the length of arr? Is 11 char arr [] = "I am happy"; // What is the length of arr2? 10 char arr2 [] = {'I', '', 'A', 'M','', 'h', 'A', 'P ', 'P', 'y', '\ 0'}; // arr and arr2 are equivalent cout <endl <arr; cout <endl <arr2; cout <endl; cout <s; return 0 ;}

Char str [5];

Cin> str;

If the length of the input string Beijing exceeds the limit, no error is reported (different from other data types), but data in other spaces is damaged, which may cause problems.

 

Use string processing functions to perform string operations

First, string connection

Use strcat (char [], const char [])

Example:

# Include <iostream> # include <string. h> using namespace std; int main () {// assign values to character arrays in C ++, ''single quotes cannot be empty char str1 [30] =" People's Republic of "; char str2 [] =" China "; cout <strcat (str1, str2 );}

Second, string Replication

# Include <iostream> # include <string. h> using namespace std; int main () {// assign values to character arrays in C ++. ''single quotes cannot be empty char str1 [30]; char str2 [] = "China"; strcpy (str1, str2); cout <str1 <"~ "<Str2 ;}

Third, string comparison

# Include <iostream> # include <string. h> using namespace std; int main () {// In the alphabetic order, there is a large ASCII char str1 [] = "Zoa "; char str2 [] = "Zoble"; int x = strcmp (str1, str2); cout <x ;}

If it is equal to 0, if it is greater than 1, if it is less than-1;

Fourth, String Length Function

# Include <iostream> # include <string. h> using namespace std; int main () {// string "Zoa", which is equivalent to {'Z', 'O', 'A ', '\ 0'} // the string length function calculates the actual length, not the actual length. char str1 [10] = "Zoa"; char str2 [30] = "Zoble "; int x = strlen (str1); int y = strlen (str2); cout <x <"~~~ "<Y ;}

 

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.