0% found this document useful (0 votes)
5 views24 pages

ArrayandString 2

The document provides an overview of arrays and strings in C++, including their syntax, types, and methods for accessing elements. It explains one-dimensional and multi-dimensional arrays, along with practical examples and exercises. Additionally, it covers C-style strings and the string class, detailing common operations and functions associated with them.

Uploaded by

anushreegoud38
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

ArrayandString 2

The document provides an overview of arrays and strings in C++, including their syntax, types, and methods for accessing elements. It explains one-dimensional and multi-dimensional arrays, along with practical examples and exercises. Additionally, it covers C-style strings and the string class, detailing common operations and functions associated with them.

Uploaded by

anushreegoud38
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arrays and String

Arrays
• Agenda for the day
• What?
• Syntax of array
• Types of array
• How to access element of array
• Using index value
• Using Loop statements
What is Array ?
• Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for
each value.
• To declare an array, define the variable type, specify the
name of the array followed by square brackets []and
specify the number of elements it should store:
Syntax
• C++ Array Declaration
dataType arrayName[arraySize];
For example,
int x[6];//declaration of array
Here,
• int - type of element to be stored
• x - name of the array
• 6 - size of the array
C++ Array Definition:
x[0]=0;
x[1]=1;
x[2]=2;….. x[5]=5;
Need to know about arrays:

Few Things to Remember:


•The array indices start with 0. Meaning x[0] is the first element stored at index 0.
•If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the last
element.
Another method to initialize array during declaration:
// declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
Here, we have not mentioned the size of the array. In such cases, the compiler automatically computes
the size.
• x[6]={1,2,3}
Types of array
• One diamensional • Two/Multi dimensional
Array Array
It declare all element Array will have rows
in 1 row formate and column formate
int test[2][2] = {
int x[6] = {19, 10, 8, {2, 4},
17, 9, 15}; {9, 0}
};
access Elements of Array using
Array index
access Elements of Array using
for loop statement
Example of two dimensional Array
Practical Practice on Array
[Link] one dimensional array for 10 value and
replace 5th value with “200”
[Link] 2*2 matrix using two dimensional array
using user input
String in C++
A string is a collection of characters. There
are two types of strings commonly used in
C++ :
Character style string : • String class
C-style strings are created Strings are used for storing
with the char type instead text/characters.
of String.
A string variable contains a
create char type array of collection of characters
characters to make a "string" surrounded by double
in C. quotes:

Char str[20]=”Hello” String str=“Hello”


Cout<<str; Cout<<Str;
Example using C style String
User input C style string
Example using String class
Example string user input
Some operations on C style
string
• strchr(str1, 'W’); Searching for a Character
• strstr(str2, "fun"); Searching for a Substring
• strncat(str3, str1, 5); // String Concatenation
with Limited Characters
• strncpy(str3, str2, 10); // String Copy with
Limited Characters
String Class Functions
The common functions that are used with the string class are.
Practical based on String
• [Link] String using C style String ”Welcome to C++
Program
Perform Following Operations:
[Link] total length of String
[Link] welcome from string
[Link] ”Btech” in existing string

• [Link] string using string class (user input ) and


perform following function
[Link] length
[Link] into another string variable i.e. str2
Thank You

You might also like