* * 济南大学信息科学与工程学院 * C++ 程序设计 主讲人:赵亚欧 Ise_zhao@ujn.edu.cn 济南大学 信息科学与技术学院 Chapter 6 Strings 6.1 C-strings(C 风格字符串)- review C-string: a string is an array of characters (elements of type char) with the null character \0 in the last element of the array The string literal Hello actually contains 6, rather than 5,characters. char greetings[6] = {H, e, l, l, o, \0}; char greetings[ ] = Hello; char greetings[ ] = \Hello\, I said.; cout greetings; 6.1 C-strings - review If specify the size of the array and the string is shorter than this size, the remaining elements of the array are initialized with the null character \0. To include a double quote inside a string precede the quote with a back slash (\). char greetings[9] = Hello; Hello, I said. The newline (\n) escape sequence can be used in place of endl to advance to a new line. If a string is too long to fit onto a single program line, it can be broken up into smaller segments. 6.1 C-strings cout some text \n; cout some textendl; char long_string[ ] = This is the first half of the string and this is the second half.; 6.2 C-string input and output Remember that the number of elements in the char array must be one more than the number of characters in the string. Program Example P6A: a simple demonstration of C-string input and output. #include iostream using namespace std; main() { const int MAX_CHARACTERS = 10; char first_name[MAX_CHARACTERS + 1]; cout Enter your first name (maximum MAX_CHARACTERS characters) ; cin first_name; cout Hello first_name endl; } ? The extraction operator read the characters up to( but not including) the space character after John. Enter your first name (maximum 10 characters) Hello John John John Paul 6.2 C-string input and output If the user does types in more than 10 characters, what will happen? The array first_name will overflow, the excess characters will overwrite other areas of memory,
您可能关注的文档
- 八年级上册《观潮》(上课)要点.ppt
- 《经济政治与社会》第14课建设和谐文化答辩.ppt
- 八年级上册28.《观潮》22要点.ppt
- 第17课时欧姆定律要点.ppt
- 《精细有机合成化学及工艺学》第3章卤化答辩.ppt
- 迪拜达芬奇塔赏析要点.ppt
- 八年级上册28.《观潮》语文简介要点.ppt
- 迪拜帆船酒店(中英文)要点.ppt
- 2柱钢筋工程量计算答辩.ppt
- 《敬业与乐业》第二课时答辩.ppt
- 2025-2026学年天津市和平区高三(上)期末数学试卷(含解析).pdf
- 2025-2026学年云南省楚雄州高三(上)期末数学试卷(含答案).pdf
- 2025-2026学年甘肃省天水市张家川实验中学高三(上)期末数学试卷(含答案).docx
- 2025-2026学年福建省厦门市松柏中学高二(上)期末数学试卷(含答案).docx
- 2025-2026学年广西钦州市高一(上)期末物理试卷(含答案).docx
- 2025-2026学年河北省邯郸市临漳县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省石家庄二十三中七年级(上)期末历史试卷(含答案).docx
- 2025-2026学年海南省五指山市九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省唐山市玉田县九年级(上)期末化学试卷(含答案).docx
- 2025-2026学年河北省邢台市市区九年级(上)期末化学试卷(含答案).docx
原创力文档

文档评论(0)