cs1010e programming methodology tutorial 8 pointers and strings
Download
Skip this Video
Download Presentation
CS1010E Programming Methodology Tutorial 8 Pointers and Strings

Loading in 2 Seconds...

play fullscreen
1 / 53

CS1010E Programming Methodology Tutorial 8 Pointers and Strings - PowerPoint PPT Presentation


  • 113 Views
  • Uploaded on

CS1010E Programming Methodology Tutorial 8 Pointers and Strings. C14,A15,D11,C08,C11,A02. Question 1. list1. list2. Question 1 (a). list1. list2. char * ptr ; ptr = list1 [ 2 ]; printf ( "\%c" , ptr );. ‘U’. 0x000000055.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'CS1010E Programming Methodology Tutorial 8 Pointers and Strings' - wes


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
question 1
Question 1

list1

list2

question 1 a
Question 1 (a)

list1

list2

char*ptr;

ptr= list1[2];

printf("%c",ptr);

‘U’

0x000000055

ptr takes in an address, it will treat ‘u’ as address of ‘0x00000055’. This may be some protected location in your system!!

‘U’

ptr

question 1 b
Question 1 (b)

list1

list2

void swap1(char*ptr1,char*ptr2){

char*dummy;

dummy = ptr1;

ptr1 = ptr2;

ptr2 = dummy;

}

swap1(&list1[2], &list1[4]);

question 1 b1
Question 1 (b)

list1

list2

void swap1(char*ptr1,char*ptr2){

char*dummy;

dummy = ptr1;

ptr1 = ptr2;

ptr2 = dummy;

}

swap1(&list1[2], &list1[4]);

Scope of swap

ptr1

ptr2

question 1 b2
Question 1 (b)

list1

list2

void swap1(char*ptr1,char*ptr2){

char*dummy;

dummy = ptr1;

ptr1 = ptr2;

ptr2 = dummy;

}

swap1(&list1[2], &list1[4]);

Scope of swap

ptr1

ptr2

dummy

question 1 b3
Question 1 (b)

list1

list2

void swap1(char*ptr1,char*ptr2){

char*dummy;

dummy = ptr1;

ptr1 = ptr2;

ptr2 = dummy;

}

swap1(&list1[2], &list1[4]);

Scope of swap

ptr1

ptr2

dummy

question 1 b4
Question 1 (b)

list1

list2

void swap1(char*ptr1,char*ptr2){

char*dummy;

dummy = ptr1;

ptr1 = ptr2;

ptr2 = dummy;

}

swap1(&list1[2], &list1[4]);

Scope of swap

ptr1

ptr2

dummy

question 1 b5
Question 1 (b)

list1

list2

void swap1(char*ptr1,char*ptr2){

char*dummy;

dummy = ptr1;

ptr1 = ptr2;

ptr2 = dummy;

}

swap1(&list1[2], &list1[4]);

Scope of swap

printf("%c %c\n", list1[2], list1[4]);

u e

ptr1

ptr2

dummy

question 1 c
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4])

question 1 c1
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4])

Scope of swap2

ptr1

ptr2

question 1 c2
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4])

Scope of swap2

dummy

ptr1

ptr2

question 1 c3
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4])

Scope of swap2

‘U’

dummy

ptr1

ptr2

question 1 c4
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4])

Scope of swap2

‘U’

dummy

ptr1

ptr2

question 1 c5
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4])

Scope of swap2

‘U’

dummy

ptr1

ptr2

question 1 c6
Question 1 (c)

list1

list2

void swap2(char*ptr1,char*ptr2){

chardummy;

dummy =*ptr1;

*ptr1 =*ptr2;

*ptr2 = dummy;

}

Swap2(&list1[2], &list1[4]);

Scope of swap2

‘U’

dummy

printf("%c %c\n", list1[2], list1[4]);

E U

ptr1

ptr2

question 1 d
Question 1 (d)
  • printf("%s", list1);
    • student
  • printf("%s", &list1[0]);
    • student
  • printf("%s", list2[3]);
    • project
  • printf("%s", &list2[3][0]);
    • project

list1

list2

“%s” will print everything until the first ‘\0’.

question 1 e
Question 1 (e)

list1

list2

char*ptr;

ptr= list2[1];

ptr++;

printf("%s",ptr);

ptr

question 1 e1
Question 1 (e)

list1

list2

char*ptr;

ptr= list2[1];

ptr++;

printf("%s",ptr);

ptr

question 1 e2
Question 1 (e)

list1

list2

char*ptr;

ptr= list2[1];

ptr++;

printf("%s",ptr);

ptr

question 1 e3
Question 1 (e)

list1

list2

char*ptr;

ptr= list2[1];

ptr++;

printf("%s",ptr);

ptr

UTORIAL

question 1 f
Question 1 (f)

list1

list2

char*ptr;

ptr=&list1[2];

*ptr++;

printf("%c %c", list1[2],ptr);

ptr

question 1 f1
Question 1 (f)

list1

list2

char*ptr;

ptr=&list1[2];

*ptr++;

printf("%c %c", list1[2],ptr);

ptr

question 1 f2
Question 1 (f)

list1

list2

char*ptr;

ptr=&list1[2];

*ptr++;

printf("%c %c", list1[2],ptr);

ptr

“++” has high priority than “*”!

question 1 f3
Question 1 (f)

list1

list2

char*ptr;

ptr=&list1[2];

*ptr++;

printf("%c %c", list1[2],ptr);

ptr

“++” has high priority than “*”!

U D

question 2
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

Not in duplicate group

Count = 0

question 21
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

Not in duplicate group

Count = 0

question 22
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

In duplicate group

Count = 0

question 23
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

In duplicate group, about to leave

Count = 1

question 24
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

Not in duplicate group

Count = 1

question 25
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

In duplicate group

Count = 1

question 26
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

In duplicate group, about to leave

Count = 2

question 27
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

Not in duplicate group

Count = 2

question 28
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

In duplicate group

Count = 2

question 29
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

In duplicate group, about to leave

Count = 3

question 210
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

End of string, return

Count = 3

question 211
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Before writing program, lets see how we do this manually

We increase our counter when:

leaving a duplicate group!

How do we detect when this?

str[i] == str[i-1] && str[i] != str[i+1]

question 212
Question 2
  • Counting duplicating groups
  • intrepeat(char str[]);
  • Algorithm
    • Count = 0;
    • For every str[i] in string str[0… len],
      • if(str[i] == str[i-1] && str[i] != str[i+1]) count++;
    • Return count;

How do we detect when this?

str[i] == str[i-1] && str[i] != str[i+1]

question 213
Question 2

int repeat(charstr[]){

intcount =0,i=1;

for(;i

&&str[i]==str[i-1]

&&str[i]!=str[i+1]){

count++;

}

}

returncount;

}

Code for counting duplicate groups

question 3
Question 3
  • Delete a substring from main string
  • Analysis:
    • To delete a substring, we need to find a string
    • To find a string, we need to check every character of string for match
    • After find a string, delete it from string
  • Suppose we have three functions:
    • findstr(mainstr, substr);
      • Find first occurrence of substr in mainstr
    • delstr(mainstr, index, sublen);
      • delete from main string at position index
question 31
Question 3

char*delete(char*mainstr,char*substr){

intindex,sublen=slength(substr);//find the position of sub string in the main string

index =findstr(mainstr,substr);

if(index==-1){//no sub string found

printf("No sub-string found! Task not successful!\n");

return0;

}else{//delete the substring

delstr(mainstr, index,sublen);

returnmainstr;

}

}

Skeleton of delete function

question 32
Question 3
  • Compute string length
    • will be used during deletion
  • Algorithm:
    • scanning from string head
    • count until you find ‘\0’;

intslength(char*str){

inti=0;

while(str[i]!='\0')

i++;

returni;

}

question 33
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Don’t match

question 34
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Don’t match

question 35
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Don’t match

question 36
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Don’t match

question 37
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Don’t match

question 38
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Don’t match

question 39
Question 3
  • Find the first occurrence of substring
  • Algorithm:
    • Scan from head of string
    • Check whether there is a substring match substr at each position
    • Return the matching position

Match & return

question 310
Question 3

intfindstr(char*mainstr,char*substr){

inti=j=equal=0;

intstart=-1, mainlen=slength(mainstr), sublen=slength(substr);

intmaxstartindex=mainlen-sublen;

//find the first character of substring in the main string

for(i=0;i<=maxstartindex&&!equal;i++)

if(substr[0]=='*'||mainstr[i]==substr[0]){

equal=1; start=i;

//compare the following characters

for(j=0; j

if(substr[j]!='*'&&mainstr[i+j]!=substr[j]){

equal=0; start=-1;

}

}

returnstart;

}

Code for findstr

question 311
Question 3
  • Delete a sequence in mainstr of length n
  • The same with deletelist from last tutorial
question 4
Question 4
  • The same with 2D matrix sorting from last tutorial.
slide53
Thank you

See you next week!!

ad