Deep parsing of character arrays and methods of handling strings in C + + _c language

Source: Internet
Author: User
Tags array length arrays constant

C + + character array
The array used to hold the character data is an array of characters, and one of the elements in the character array holds one character. A character array has a common attribute of an array. Because of the wide range of string applications, C and C + + specifically provide it with many convenient usages and functions.
The definition and initialization of a character array

The method for defining a character array is similar to the previous description. For example:

  Char c[10];
  c[0]=′i′;c[1]=′′;c[2]=′a′;c[3]=′m′;c[4]=′′;c[5]=′h′;c[6]=′a′;c[7]=′p′;c[8]=′p′;c[9]=′y′;

c is defined as an array of characters and contains 10 elements. The state of the array after the assignment is shown in the figure.


To initialize a character array, the easiest way to understand it is to assignments the elements of the array by word-by-character. Such as

  Char c[10]={' I ', ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y '};


Assign 10 characters to each of the 10 elements of c[0]~c[9].

If the number of initial values provided in the curly braces is greater than the array length, the syntax is handled incorrectly. If the number of initial values is less than the length of the array, only those characters are assignments to the preceding elements in the array, and the remaining elements are automatically set to null characters. If you provide the same number of initial values as the predetermined array length, you can omit the length of the array when you define it, and the system automatically determines the length of the array based on the number of initial values. Such as:

  Char c[]={' I ', ', ' a ', ' m ', ', ' h ', ' A ', ' P ', ' P ', ' Y '};


You can also define and initialize a two-dimensional character array, such as

Copy Code code as follows:
Char diamond[5][5]={{', ', ', '},{', ' * ', ', ', ' * '},{', ', ', ', ', ', ' '},{', ' * ', ', ' ', '},{', ', ', ', '};

Assignment and reference of a character array

You can assign values only to the elements of a character array, not to the entire array with an assignment statement. Such as:

  Char c[5];
  c={' C ', ' h ', ' I ', ' n ', ' a '}; Error, cannot assign value to entire array
  c[0]= ' C '; c[1]= ' h '; c[2]= ' I '; c[3]= ' n '; c[4]= ' a ';

If A and B have been defined as arrays of the same type and length, and b arrays have been initialized, analyze:

  A=b; Error, cannot assign value to whole array
  a[0]=b[0];//correct, referencing array elements

"Example" design and output a diamond graphic.

#include <iostream>
using namespace std;
void Main ()
{
  char diamond[][5]={{', ' ', ' * '},{', ', ', ', ', ', ' '},{', ', ', ', ', ' ', ', ', ', ', ', ', ', ' ', ' ', ' ', ' ', ' ', ' '},{', ' ,'*'}};
  int i,j;
  for (i=0;i<5;i++)
  {
   for (j=0;j<5;j++)
   cout<<diamond[i][j];//reference array element, one character at a time
   cout <<endl;
  }
}

C + + methods for handling strings-string classes and string variables
storing strings with character arrays is not the most ideal and safest method.

C + + provides a new data type--the string type (string type), which, as with the char, int type, can be used to define the variable, which is the string variable--a character sequence with a name.

In fact, string is not the basic type that the C + + language itself has, it is a string class declared in the C + + standard library, in which you can define objects. Each string variable is an object of the string class.
Definition and reference of string variables

1) Define string variables
As with other type variables, a string variable must be defined before it is used, and the string variable is defined with the class name string. Such as:

  string string1; Defines string1 as a string variable string
  String2=″china″//definition string2 to initialize it at the same time

It should be noted that to use the functions of the string class, you must include the string header in the C + + standard library at the beginning of this file, which should be added

  #include <string>//Note header filename is not string.h

2 assignment to a string variable
After you define a string variable, you can assign it a string constant with an assignment statement, such as:

  String1=″canada″;


You can assign a value to a string variable with a string constant, or you can assign a value to another string variable with a string variable. Such as:

  String2=string1; Assume that both string2 and string1 are defined as string variables


Do not require string2 and string1 length is the same, if string2 is originally ″china″,string1 is ″canada″, after assigning string2 also become ″canada″. You do not specify a length when you define a string variable, and the length changes with the length of the string.

You can manipulate a character in a string variable, such as:

  String Word=″then″; Define and initialize the string variable word
  word[2]=′a′;//Modify the character with ordinal 2, and the value of Word is ″than″

3 input and output of string variables
You can enter an output string with the string variable name in the input and output statement, such as:

  Cin>> string1; Input a string from the keyboard to the string variable string1
  cout<< string2;//Output String string2


Operation of String variables

As you can see in the previous section, when you hold a string in a character array, the operation of the string takes a string function, such as strcat (join), strcmp (comparison), strcpy (copy), and a String class object, which can be used directly with a simple operator.

1) string Copy with assignment number

  String1=string2;

Its function with "strcpy (string1,string2);" Same.

2) string concatenation with plus sign

  String String1=″c++″; Define string1 and assign an initial value
  string string2=″language″;//define string2 and assign an initial value
  String1=string1 + string2;//connection string1 and string2


After the connection string1 is ″c++ Language″.

3) string comparison directly with the relational operator
A string comparison can be made directly with a relational operator such as = = (equal to), > (greater than), < (less than),!= (not equal to), >= (greater than or equal to), <= (less than or equal).
Array of strings

Not only can you define string variables with string, you can also define string arrays with string. Such as:

  String name[5]; Defines an array of strings containing 5 string elements string
  Name[5]={″zhang″,″li″,″fun″,″wang″,″tan″};//define an array of strings and initialize

The status of the name array is as shown in the figure.


A few notes on the string array:
Contains several (now 5) elements in an array of strings, each equivalent to a string variable.
Each string element is not required to have the same length, and even the length of the same element can vary, and when a value is assigned to an element, its length may change.
Holds a string in each element of the string array, rather than a character, which is the difference between an array of strings and a character array. If a character array is used to hold a string, an element can hold only one character and hold a string in a one-dimensional character array.
Each string element contains only the characters of the string itself, excluding ′\0′.

It is convenient to hold strings in string arrays and to handle strings.

How do you allocate storage space to an array when defining a string array? In fact, the compilation system assigns 4 bytes to each string variable, in which the string itself is not stored directly, but rather the address of the string. In this case, the address of the string ″zhang″ is stored in name[0], the address of the string ″li″ is stored in name[1, and the address of the string ″fun″ is stored in name[2] ... On just a schematic. In a string variable, a pointer to a string (the address of the string) is stored.

Related Article

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.