Cs1010e programming methodology tutorial 8 pointers and strings
This presentation is the property of its rightful owner.
Sponsored Links
1 / 53

CS1010E Programming Methodology Tutorial 8 Pointers and Strings PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

CS1010E Programming Methodology Tutorial 8 Pointers and Strings

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


Cs1010e programming methodology tutorial 8 pointers and strings

CS1010E Programming MethodologyTutorial 8Pointers and Strings

C14,A15,D11,C08,C11,A02


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<strlen(str);i++){if(!isspace(str[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<sublen; 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.


Cs1010e programming methodology tutorial 8 pointers and strings

Thank you

See you next week!!


  • Login