1 / 18

Introduction to Computer Algorithmics and Programming Ceng 113

Introduction to Computer Algorithmics and Programming Ceng 113. Serap ATAY <serapatay@iyte.edu.tr>. Pointers _ Part 2. What is wrong?. *s + 1 = ‘x’; *(s+1) = ‘x’; int p; .... *p=‘a’; c = a * *b; char ch; char *p; ... ch = ‘a’; p = &ch;. What is wrong?. ERROR;

Télécharger la présentation

Introduction to Computer Algorithmics and Programming Ceng 113

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. Introduction to Computer Algorithmics and ProgrammingCeng 113 Serap ATAY <serapatay@iyte.edu.tr> Pointers _ Part 2

  2. What is wrong? • *s + 1 = ‘x’; • *(s+1) = ‘x’; • int p; .... *p=‘a’; • c = a * *b; • char ch; char *p; ... ch = ‘a’; p = &ch;

  3. What is wrong? ERROR; The address operator can be used with only objects. MAX is only a logical constant. #define MAX 100 .. char *p; ... p = &MAX;

  4. Pointer increment/decrement char *p; int a; ... p=p + a; p=p – 2; p += a; p= p + a + 10; ++p;

  5. Sample “Swap operation” #include void swap(int *x, int *y); void main() { int a=10, b= 20; swap(&a, &b); printf(“a= %d b = %d \n”, a, b); } void swap(int *x, int *y) { int temp; temp = *x; *x=*y; *y= temp; }

  6. Sample“Array operation” #include <stdio.h> int max(int *p, int n); void main() { int a[10]={10,-4,5,9,71,98,89,4,8, 10}; int m; m= max(a, 10); printf(“The greatest number=%d \n”, m); } int max(int *p, int n) { int maxval, k; maxval = p[0]; for (k=1; k <n; ++k) if (maxval < p[k]) maxval = p[k]; return maxval; } int max(int *p, int n) { int maxval, k; maxval = 0; for (k=0; k <n; ++k) { if (maxval < *p) maxval = *p; p++; } return maxval; }

  7. Sample“Array operation”

  8. Sample #include <stdio.h> void getname(char *s); void main() { char str[50]; getname(str); printf("%s \n", str); } void getname(char *s) { printf("Name - Last name:"); gets(s); }

  9. Sample “Split String” Suppose we are given a string of the form "Last_name/First_name." We want to split this into two strings, one containing the first name and one containing the last name. We need a function to find the slash in the name. my_strchr  Finds a character in a string. Parameters: string_ptr String to look through. find Character to find. Returns: pointer to 1st occurrence of character in string or NULL for error.

  10. Sample “Split String”

  11. Sample “Split String” #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char line[80]; /* The input line */ char *first_ptr; /* pointer to the first name */ char *last_ptr; /* pointer to the last name */   gets(line);   last_ptr = line; /* last name is at beginning of line */   first_ptr = my_strchr(line, '/'); /* Find slash */   /* Check for an error */ if (first_ptr == NULL) { printf("Error: Unable to find slash in %s\n", line); exit (0); }   *first_ptr = '\0'; /* Zero out the slash */   ++first_ptr; /* Move to first character of name */   printf("First:%s Last:%s\n", first_ptr, last_ptr); return (0); }

  12. Sample “Split String” char *my_strchr(char *string_ptr, char find) { while (*string_ptr != find) {   /* Check for end */   if (*string_ptr == '\0') return (NULL); /* not found */   ++string_ptr; } return (string_ptr); /* Found */ }

  13. Sample _ String Copy // This program is copied one string to another and use pointers. # include <stdio.h> # include <string.h> void string_copy(char *source, char *dest); void main () { char str_from[80], str_to[80]; gets(str_from); str_to[0]='\0'; string_copy(str_from, str_to); puts(str_to); } void string_copy(char *source, char *dest) { while *source != '\0'); { *dest++ = *source++; } *dest = '\0'; } void string_copy(char *source, char *dest) { while *source != '\0'); { *dest = *source; dest++; source++; } *dest = '\0'; } void string_copy(char *source, char *dest) { while ((*dest++ = *source++) != '\0'); }

  14. Sample – “String Compare” #include <stdio.h> #include <string.h> int str_compare(char *str1, char *str2); void main() { char s1[20], s2[20]; int result=0; printf(“Input the first string: \n”); gets(s1); printf(“Input the second string: \n”); gets(s2); result = str_compare(s1, s2); if (result == 0) printf(“The strings are same.\n”); else printf(“The strings are different. \n”); } int str_compare(char *str1, char *str2) { while (*str1 == *str2) { if (*str1 == ‘\0’) return 0; ++str1; ++str2; } return *str1 - *str2; }

  15. Homework • Write an algorithm and C code to count the vowels and letters in a string. • Read a string from the screen. • Count the vowels and letters • Then print out the • number of occurrences of each of the vowels a, e, ı, i, o, ö, ü and u in the text, • the total number of letters, and • each of the vowels as an integer percentage of the letter total. • Suggested output format is: Numbers of characters: a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17 Percentages of total : a 13%; e 8%; i 0%; o 4%; u 0% ; rest 73% • Use pointers for all string, comparison and calculation operations. Upload to FTP until December 14th. 2006

  16. Quiz Question Write a program, which is concatenate second string to the end of the first string. For example: string_1= “Happy” string_2=“New year” After operation string_1=“HappyNew year” • Use pointers for all array operations. • Do not use “strlen” standard function.

  17. String Concatenate #include <stdio.h> #include <string.h> char *string_cat(char *s1, char *s2); void main() { char str1[50], str2[50]; char *p; gets(str1); gets(str2); p = string_cat(str1, str2); printf("%s \n", p); } char *string_cat(char *s1, char *s2) { char *temp; temp = s1; while (*s1 != '\0') s1++; while ((*s1++ = *s2++)!= '\0'); return temp; }

  18. Next Course • Functions

More Related