"Studious C + + 2nd Edition", chapter 5th string--Analysis text

Source: Internet
Author: User

Text string storage: Each character is prefixed with an ASCII code of ' \ S '. So a text string is a sequence of bytes, and each byte is a numeric value of 0~255. A conversion between a numeric value and a character occurs when the keyboard input or the screen output. (The International standard Unicode encoding uses more than one byte to represent each character.) )

Because the source code is saved in a text file, the text character is stored as a numeric value. Therefore, when the computer to process the source code, the processing is actually the number of these strings, according to another set of precise rules for another form of numerical operations, data evaluation and decision-making judgment. (!!! The beginning of the first version of the compiler can only be written by machine code!!! And the old compiler can also be used to write a new compiler, so the wave development, the complex compiler of course no machine code to write! )

The string is actually an array of the base type char (one-byte-wide shaping).

Char str[20] = "how long?"; Uninitialized member, whether the global situation is 0, the local situation is garbage?

The string ends with ' + ' (that is, the value 0).

Char s[] = "Const"; If the length is not specified, it is allocated 8 byte space (including '//it ') and S is constant: The specified length is truncated?

char* s = "variable"; S is a variable that can be re-assigned to point elsewhere. It: So the const char* s can be declared, so that s is immutable.

String handler function:

strcpy (S1,S2)//s2 copy to S1

strcat (S1,S2)//s2 append to S1

strncpy (s1,s2,n)//copy up to N, not including '//s2 ' is a constant string and there's ' s ', what happens?

Strncat (S1,s2,n)//up to n additional, excluding '/'. The append starting point is the location of the first ' x ' in S1

Strlen (s)//s length, not including '/'

Strtok (SRC_STR,DELIMS)//split src_str with character Delims, returns a pointer to the first substring

Strtok (NULL, delims)//Returns a pointer to the next substring in the known source string (Strtok Last call to the specified SRC_STR). Returns NULL if no substring can be returned

C + + will not do anything in order to save the entire string, this is the code farm guarantee.

Char s[80]; Specified length but not initialized, C + + retains 80 bytes for it

When a constant string appears in the code, C + + allocates space for it and returns its address, that is, the string is evaluated as an address.

#include <iostream>

#include <cstring>//strcpy functions are here, the head.

#define STR_LEN = 100;

using namespace Std;

char* Str[str_len];

Cin.getline (str, str_len-1); Reads a line (reads ' \ n ') and reads up to str_len-1 characters. (it) should not automatically fill up '

Escape character ' \b ' is backspace

Input stream operator (>>) for automatic type conversion

Cin.getline indicates that Getline is a member function of the CIN object. The object is the object of its member function.

cin>>val; Gets the data before the first whitespace character (Space tab newline), which attempts to convert the data to Var. The data after the whitespace is left in the input stream waiting for the next input operation to take it away. If you enter a blank character directly, Cin waits for input. A better approach is to set a default value and prompt the input.

Cin.getline is often problematic after you use cin>>, because they are different in their approach to newline characters. So it's best to use only one in the same program.

#include <cstdlib>//atoi functions such as Atof

#include <cctype>//ToLower functions such as ToUpper

C Strings: Strings in C and C + + ending with '/' (char* type)

Similar to the new version of Basic,c++ provides a string type (in fact, the STL provides a class, only the very old C + + version does not support STL), so no longer consider the string length of the cumbersome and error-prone things. #include <string> can activate this support. (Note that activating the C string is required for older functions such as <cstring>,strcpy) similar to CIN and cout, you can choose using namespace std, otherwise you will std::string use this class. It:<string> belongs to the C + + standard library and is not a standard library. Also, C's standard libraries (for example, <ctype.h>) are implemented in C + + (for example, <cctype>)

String A, B, C; Statement

String A ("so"), B ("Easy"); Initialization

A = "so";  b = "Easy"; Initialization

String a = "so"; Declaring and initializing

A can be re-copied without worrying about accommodating:

A = "fking beauty is nature.";

Can be compared:

int ret = (A = = B);

"Studious C + + 2nd Edition", chapter 5th string--Analysis text

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.