KEMBAR78
DSA-Lecture03-Pointers, Strings(outputs).pptx
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++)
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.
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
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] .

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> usingnamespace 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.. intx= 10; int *ptr; ptr= &x; cout<<ptr<<endl; cout<<ptr++<<endl; cout<<ptr;
  • 6.
  • 7.
    Pointers & Arrays intarr[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 & Arrayscontd.. 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 & Arrayscontd.. 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 charstr[]= "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. KanwalYousaf 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. KanwalYousaf 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. KanwalYousaf 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 – BuiltInFunctions 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;
  • 21.
  • 23.
  • 24.
    Q1. What isthe 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