0% found this document useful (0 votes)
1 views

String and Arrays

The document provides an introduction to strings and arrays in C++ programming, referencing 'Modern C++ for Absolute Beginners' by Slobodan Dmitrović. It explains how to use the <string> header, concatenate strings using operators, and accept string input with the getline function. Additionally, it covers the declaration, initialization, and access of array elements in C++.

Uploaded by

macarullo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

String and Arrays

The document provides an introduction to strings and arrays in C++ programming, referencing 'Modern C++ for Absolute Beginners' by Slobodan Dmitrović. It explains how to use the <string> header, concatenate strings using operators, and accept string input with the getline function. Additionally, it covers the declaration, initialization, and access of array elements in C++.

Uploaded by

macarullo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CCIT 102 – COMPUTER PROGRAMMING 1

String and
Arrays
Source: Modern C++ for Absolute Beginners by Slobodan Dmitrović

C A M A R I N E S S U R P O LY T E C H N I C C O L L E G E S | C O L L E G E O F C O M P U T E R S T U D I E S
Introduction to String

String
To use string type, we need to include the <string> header in our
program:

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Concatenating String

String
We can add a string literal to our string using the compound
operator += :

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Concatenating String

String
We can add another string to our string using the + operator. We
can say we concatenate strings:

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


String Input

String
Preferred way of accepting a string from the standard input is via
the getline function which takes cin and the string as parameters:

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


CCIT 102 – COMPUTER PROGRAMMING 1

Arrays
Source: Modern C++ for Absolute Beginners by Slobodan Dmitrović

C A M A R I N E S S U R P O LY T E C H N I C C O L L E G E S | C O L L E G E O F C O M P U T E R S T U D I E S
Array

Arrays
Arrays are sequences of objects of the same type. We can declare
an array of type char and as follows:

To initialize an array, we can use the initialization list{} :

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Array

Access Elements in C++ Array


In C++, each element in an array is associated with a number. The number
is known as an array index. We can access elements of an array by using
those indices.

//syntax to access array elements


array[index] ;

Consider the array arr we have define earlier


Array Members arr[0] arr[1] arr[2] arr[3] arr[4]

Array Indices 0 1 2 3 4

Elements of an array in C++

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Array

Displaying Array Element

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER

You might also like