tailieunhanh - C++ Weekend Crash Course phần 4
Nếu nó không xuất hiện, C hoạt động như thể nó là hiện tại, nhưng trống rỗng. Trên thực tế các dấu ngoặc là tùy chọn nếu có chỉ là một tuyên bố được thực thi như một phần của vòng lặp trong khi, tuy nhiên, nó là quá dễ dàng để làm cho một sai lầm mà các trình biên dịch C có thể không bắt mà không có các dấu ngoặc như | Session 11 The Array 141 displaystring - display a character string one character at a time void displayString char sArray for int i 0 sArray i 0 i cout sArray i The bolded declaration of cMyName declares the character array with the extra character 0 on the end. The displaystring program iterates through the character array until a null character is encountered. The function displayString is simpler to use than its displayCharArray predecessor. It is no longer necessary to pass along the length of the character array. Further displayString works when the size of the character string is not known at compile time. For example this would be the case if the user were entering a string of characters from the keyboard. I have been using the term string as if it were a fundamental type such as int or float. At the time of its introduction I mentioned that string is actually a variation of an existing type. As you see here a string is a null-terminated character array. C provides an optional more convenient means of initializing a string by using double quotes rather than the single quotes used for characters. The line char szMyName Stephen Part III-Saturday Afternoon Session 11 is exactly equivalent to the line char cMyName S t e p h e n 0 in the previous example. The naming convention used here is exactly that a convention. C does not care however the prefix sz stands for zero-terminated string. The string Stephen is eight characters long not seven the null character after the n is assumed. 142 Saturday Afternoon 10 Min. To Go Manipulating Strings The C programmer is often required to manipulate strings. Although C provides a number of string manipulation functions let s write our own to get an idea of how these functions might work. Our own concatenate function Let s begin with a simple if somewhat lengthy C program to concatenate two strings. Concatenate - concatenate two strings with a - in the middle include include the following include file is .
đang nạp các trang xem trước