Download presentation
Presentation is loading. Please wait.
Published byEzra Lee Modified over 8 years ago
1
Introduction to Computer Algorithmics and Programming Ceng 113 Serap ATAY Pointers _ Part 2
2
What is wrong? 1.*s + 1 = ‘x’; 2.*(s+1) = ‘x’; 3.int p;.... *p=‘a’; 4.c = a * *b; 5.char ch; char *p;... ch = ‘a’; p = &ch;
3
What is wrong? #define MAX 100.. char *p;... p = &MAX; ERROR; The address operator can be used with only objects. MAX is only a logical constant.
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 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 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
#include 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 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 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 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.