SlideShare a Scribd company logo
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<<*ptr<<endl;
cout<<ptr<<endl;
cout<<*ptr;
cout<<*ptr<<endl;
cout<<(*ptr)++<<endl;
cout<<x<<endl;
Pointers & Arrays
int arr[2]={6,87};
int* p1= &arr[0];
cout<<*p1<<" " <<p1<<endl;
cout <<*arr<<" " <<arr;
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..
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++)
DSA-Lecture03-Pointers, Strings(outputs).pptx
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.
DSA-Lecture03-Pointers, Strings(outputs).pptx
12/07/2024 Engr. Kanwal Yousaf 17
String is a sequence of consecutive characters which ends with ‘0’.
String Operations along with the syntax can be:
String Operations
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);
12/07/2024 Engr. Kanwal Yousaf 18
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
Returns < 0, if (String 1 < String 2)
Returns =0, if (String1 == String 2)
Returns >0, if (String 1> String 2)
S1=‘Hello’, S2= ‘Pakistan’ then ?
COMPARE(S1, S2)=?
S1=‘Welcome to Java’, S2= ‘Welcome to C’
then
COMPARE(S1, S2)=?
Copy To copy the characters of one string to the
another
S1=‘Data Structures’, S2=‘Algorithms’
Then Copy_String=(S1, S2) = ‘Algorithms’
Substring Display the part of string SUBSTRING(‘The End’, 4, 4)=?
String Operations (Cont…)
12/07/2024 Engr. Kanwal Yousaf 19
Value
Relation between Compared
String and Comparing String
0 They compare equal
<0
Either the value of the first character
that does not match is lower in
the compared string, or all compared
characters match but the compared
string is shorter.
>0
Either the value of the first character
that does not match is greater in
the compared string, or all compared
characters match but the compared
string is longer.
String Comparison
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
DSA-Lecture03-Pointers, Strings(outputs).pptx
Thank you
Q1. What is the difference between function member
that are virtual and those that are not?
Q2. Write an algorithm or Pseudo code that Counts al
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] .

More Related Content

PPTX
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
PPTX
3 (3)Arrays and Strings for 11,12,college.pptx
PPT
C++ Strings.ppt
PPTX
Lecture 5Arrays on c++ for Beginner.pptx
PDF
oodp elab.pdf
PDF
I have written the code but cannot complete the assignment please help.pdf
PPTX
C programming
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
3 (3)Arrays and Strings for 11,12,college.pptx
C++ Strings.ppt
Lecture 5Arrays on c++ for Beginner.pptx
oodp elab.pdf
I have written the code but cannot complete the assignment please help.pdf
C programming
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...

Similar to DSA-Lecture03-Pointers, Strings(outputs).pptx (20)

PPT
strings
PPT
Unit 6 pointers
PDF
sodapdf-converted into ppt presentation(1).pdf
PPT
Encryption and Decryption using Tag Design
PPT
Lecture#9 Arrays in c++
PPTX
Lecture 2: arrays and pointers
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
DOCX
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
PDF
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
PPTX
C++ Pointer | Introduction to programming
PDF
C++ Nested loops, matrix and fuctions.pdf
PDF
Дмитрий Верескун «Синтаксический сахар C#»
PPT
Pointers+(2)
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PDF
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
PDF
Im having difficulty with the directives i figured out a duplicatio.pdf
PDF
Stl algorithm-Basic types
PDF
Arduino coding class part ii
PDF
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
PPTX
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
strings
Unit 6 pointers
sodapdf-converted into ppt presentation(1).pdf
Encryption and Decryption using Tag Design
Lecture#9 Arrays in c++
Lecture 2: arrays and pointers
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
C++ Pointer | Introduction to programming
C++ Nested loops, matrix and fuctions.pdf
Дмитрий Верескун «Синтаксический сахар C#»
Pointers+(2)
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
Im having difficulty with the directives i figured out a duplicatio.pdf
Stl algorithm-Basic types
Arduino coding class part ii
Pointer in C++_Somesh_Kumar_Dewangan_SSTC
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
Ad

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
additive manufacturing of ss316l using mig welding
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
composite construction of structures.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Well-logging-methods_new................
PPTX
Geodesy 1.pptx...............................................
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
additive manufacturing of ss316l using mig welding
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
composite construction of structures.pdf
Foundation to blockchain - A guide to Blockchain Tech
Well-logging-methods_new................
Geodesy 1.pptx...............................................
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Current and future trends in Computer Vision.pptx
Safety Seminar civil to be ensured for safe working.
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Internet of Things (IOT) - A guide to understanding
Ad

DSA-Lecture03-Pointers, Strings(outputs).pptx

  • 1. 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
  • 2. 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)
  • 3. Today’s Outline -- Strings (in C++)
  • 4. 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; }
  • 5. Pointer Arithmetic contd.. int x= 10; int *ptr; ptr= &x; cout<<ptr<<endl; cout<<ptr++<<endl; cout<<ptr;
  • 7. Pointers & Arrays int arr[2]={6,87}; int* p1= &arr[0]; cout<<*p1<<" " <<p1<<endl; cout <<*arr<<" " <<arr;
  • 8. 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..
  • 9. 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;
  • 10. 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]<<" ";}
  • 11. Pointers and Strings char str[]= "computer"; char *ptr= str; cout<<str<<endl; cout<<*ptr<<endl;
  • 12. 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;}
  • 13. Today’s Outline -- Strings (in C++)
  • 15. 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.
  • 17. 12/07/2024 Engr. Kanwal Yousaf 17 String is a sequence of consecutive characters which ends with ‘0’. String Operations along with the syntax can be: String Operations 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);
  • 18. 12/07/2024 Engr. Kanwal Yousaf 18 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 Returns < 0, if (String 1 < String 2) Returns =0, if (String1 == String 2) Returns >0, if (String 1> String 2) S1=‘Hello’, S2= ‘Pakistan’ then ? COMPARE(S1, S2)=? S1=‘Welcome to Java’, S2= ‘Welcome to C’ then COMPARE(S1, S2)=? Copy To copy the characters of one string to the another S1=‘Data Structures’, S2=‘Algorithms’ Then Copy_String=(S1, S2) = ‘Algorithms’ Substring Display the part of string SUBSTRING(‘The End’, 4, 4)=? String Operations (Cont…)
  • 19. 12/07/2024 Engr. Kanwal Yousaf 19 Value Relation between Compared String and Comparing String 0 They compare equal <0 Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter. >0 Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer. String Comparison
  • 20. 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;
  • 24. Q1. What is the difference between function member that are virtual and those that are not? Q2. Write an algorithm or Pseudo code that Counts al 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] .

Editor's Notes

  • #18: Ascii Value of H= 72 (decimal) C= 67, J=74 Ascii Value of P= 80