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

DSA-Lecture03-Pointers, Strings (Outputs)

Uploaded by

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

DSA-Lecture03-Pointers, Strings (Outputs)

Uploaded by

Engr Sameer Hani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Today’s Agenda --

1. Pointers
◦ Chapter 1 DSA 4th Ed by Adam Drozdek
◦ SlideShare handouts
2. Strings
◦ Chapter 1 DSA 4th Ed by Adam Drozdek
◦ SlideShare handouts

3. Quiz
Today’s Outline -- Pointers (in C++)

◦ Introduction (Recap)
◦ Pointer Arithmetic
◦ Pointer & Arrays
◦ Reference Variables
◦ Pointers in Functions
◦ New, Delete Function

(Chapter 1 by Adam Drozdek)


Today’s Outline -- Strings (in C++)
Pointer Arithmetic
#include <iostream>
using namespace std;
int main() {
int a=67;
int* p1= &a;
cout<<" original p is address of a " <<p1;
p1++;
cout<<" \n now p is next to a like " <<p1;
return 0; }
Pointer Arithmetic contd..
int x= 10;
int *ptr;
ptr= &x;
cout<<ptr<<endl;
cout<<ptr++<<endl;
cout<<ptr;
Pointer Arithmetic contd..
cout<<*ptr<<endl;
cout<<++*ptr<<endl; cout<<*ptr<<endl;
cout<<(*ptr)++<<endl;
cout<<x<<endl;

cout<<*ptr<<endl;
cout<<*ptr++<<endl;
cout<<*ptr<<endl;
cout<<ptr<<endl;
cout<<*ptr;
Pointers & Arrays

int arr[2]={6,87};
int* p1= &arr[0];
cout<<*p1<<" " <<p1<<endl;
cout <<*arr<<" " <<arr;
Pointers & Arrays contd..

int arr[2]={6,87};
int* p1= &arr[0];
cout <<p1 << " " <<*p1;
cout<<" \n " << *p1++ <<" " <<*p1;
_______________________________________________________

int arr[2]={6,87};
int* p1= &arr[0];

cout <<p1 << " " <<*p1;


cout<<" \n " << (*p1)++ <<" " <<*p1;
Pointers & Arrays contd..
int arr[2]={6,87};
int* p1= &arr[0];
cout <<p1 << " " <<*p1;
cout<<" \n " << *++p1 <<" " <<*p1;
_________________________________________________
int arr[2]={6,87};
int* p1= &arr[0];

cout <<p1 << " " <<*p1;


cout<<" \n " << ++*p1 <<" " <<*p1;
Pointers & Arrays contd..
void scan(int* p, int n)
{ for (int i =0; i<n; i++)
cout<<*(p+i)<<"\n";}
int main() {
int arr[4]= {1, 2, 7, 8};
scan(arr, 4);
_______________________________________________
void scan(int* p, int n)
{ for (int i =0; i<n; i++)
cout<<*(p+i)<<"\n";
*(p+2)=3;}
int main() { int arr[4]= {1, 2, 7, 8};
scan(arr, 4);
for(int j=0;j<4;j++)
{cout<< arr[j]<<" ";}
Pointers and Strings
char str[]= "computer";
char *ptr= str;
cout<<str<<endl;
cout<<*ptr<<endl;
Array of Pointers
char *ptr[] = { "car", "bus", "bike", "truck"};
cout<<*ptr;

char *ptr[] = { "car", "bus", "bike", "truck"};


cout<<*ptr<<endl;
for(int i=0; i<4; i++)
{ cout<<*ptr[i]<<endl;}
Today’s Outline -- Strings (in C++)
String Class
In C++, you can also create a string object for holding strings.
Unlike using char arrays, string objects has no fixed length, and
can be extended as per your requirement.
String Operations
String is a sequence of consecutive characters which ends with ‘\0’.
String Operations along with the syntax can be:
Operations Traditional Syntax

Length Length(string);
Concatenation Concate(string1, string2);

Comparison Compare(string1, string2);

Copy CopyString(string1, string2);

Substring Substring(string, strt_position_int, end_position_int);

11/07/2024 Engr. Kanwal Yousaf 17


String Operations (Cont…)
Operations Description Example

Length The number of characters in the string LENGTH(‘COMPUTER’)=8,


LENGTH(‘ ’)=?

Concatenation To combine two strings into one. S1=‘Hello’, S2=‘Pakistan’ then


S1 + S2= ‘HelloPakistan’

But
S1+ ‘ ’ + S2= ‘Hello Pakistan’

Comparison To compare any two strings S1=‘Hello’, S2= ‘Pakistan’ then ?


COMPARE(S1, S2)=?
Returns < 0, if (String 1 < String 2)
S1=‘Welcome to Java’, S2= ‘Welcome to C’
Returns =0, if (String1 == String 2) then

Returns >0, if (String 1> String 2) COMPARE(S1, S2)=?

Copy To copy the characters of one string to the S1=‘Data Structures’, S2=‘Algorithms’
another Then Copy_String=(S1, S2) = ‘Algorithms’

Substring Display the part of string SUBSTRING(‘The End’, 4, 4)=?

11/07/2024 Engr. Kanwal Yousaf 18


String Comparison
Relation between Compared
Value
String and Comparing String
0 They compare equal

Either the value of the first character


that does not match is lower in
<0 the compared string, or all compared
characters match but the compared
string is shorter.

Either the value of the first character


that does not match is greater in
>0 the compared string, or all compared
characters match but the compared
string is longer.

11/07/2024 Engr. Kanwal Yousaf 19


String – BuiltIn Functions
string str;
str="DSA lecture";
int l=str.length();
cout<<l;

string str;
str="DSA lecture";
int l=str.length();
cout<<l<<endl;
int pat= str.find('A');
cout<<pat;
Strings
Thank you
Q1. What is the difference between function
members that are virtual and those that are not?
Q2. Write an algorithm or Pseudo code that Counts
all zeros, Odd numbers and even number in a linear
array.
Q3. Consider 2D.Linear arrays AAA[4][5].
(a) Find the total number of elements.
(b) Suppose Base(AAA) = 300 and w=4 words per
memory cell for AAA. Find the address of AAA[3][2] .

You might also like