1 / 53

CS1010E Programming Methodology Tutorial 8 Pointers and Strings

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.

wes
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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010E Programming MethodologyTutorial 8Pointers and Strings C14,A15,D11,C08,C11,A02

  2. Question 1 list1 list2

  3. 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

  4. Question 1 (b) list1 list2 void swap1(char*ptr1,char*ptr2){ char*dummy; dummy = ptr1; ptr1 = ptr2; ptr2 = dummy; } swap1(&list1[2], &list1[4]);

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. Question 1 (c) list1 list2 void swap2(char*ptr1,char*ptr2){ chardummy; dummy =*ptr1; *ptr1 =*ptr2; *ptr2 = dummy; } Swap2(&list1[2], &list1[4])

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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’.

  18. Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr

  19. Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr

  20. Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr

  21. Question 1 (e) list1 list2 char*ptr; ptr= list2[1]; ptr++; printf("%s",ptr); ptr UTORIAL

  22. Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr

  23. Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr

  24. Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr “++” has high priority than “*”!

  25. Question 1 (f) list1 list2 char*ptr; ptr=&list1[2]; *ptr++; printf("%c %c", list1[2],ptr); ptr “++” has high priority than “*”! U D

  26. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 0

  27. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 0

  28. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group Count = 0

  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 = 1

  30. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 1

  31. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group Count = 1

  32. 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

  33. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually Not in duplicate group Count = 2

  34. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually In duplicate group Count = 2

  35. 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

  36. Question 2 • Counting duplicating groups • intrepeat(char str[]); • Before writing program, lets see how we do this manually End of string, return Count = 3

  37. 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]

  38. 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]

  39. 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

  40. 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

  41. 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

  42. 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; }

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related