Download presentation
Presentation is loading. Please wait.
Published byAlfonso Oddy Modified over 9 years ago
1
array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
2
Pointer arithmetic strcpy void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; }
3
Exercise Write a program that gets a string from the user and checks whether or not it is a palindrome Example for a palindrome: abbcbba (Hint: use strlen…)
4
Palindrome.c /* This program checks whether a given string is a palindrome*/ #include int main(void) { int len,i; char str[101]; printf("Enter a string\n"); scanf("%100s",str); len = strlen(str); for (i=0; i<len/2; i++) if (str[i] != str[len-i-1]) { printf("The string is not a palindrome!\n"); return 0; } printf("The string is a palindrome!\n"); return 0; }
5
An additional use for pointers A function that accepts an array and searches for something within it (a char, a number, etc.) What should the function return? The index where the ‘something’ was found A pointer to the place where it was found
6
Functions that return pointers Like any other data type, functions can return pointers For example, a prototype for a function returning a pointer to char will be – char *func(…); But how would we indicate failure Suppose we searched the array and didn’t find anything – what should we return?
7
strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
8
Mystery – what does this do? int main(void) { char s[LENGTH+1]; char *p; scanf("%100s", s); p = strchr(s, ','); while(p!=NULL) { *p = ' '; p = strchr(p+1, ','); } printf("The result is - %s\n", s); return 0; }
9
Exercise Implement the string.h function strrchr that returns a pointer to the last occurrence of a character inside a string. Write a program that accepts a string and char from the user, displays what remains of the string after the last occurrence of that char
10
Solution strrchr.c
11
strncmp Declared in string.h, with prototype int strncmp(char *s1, char *s2, int n); Returns 0 if the first n letters of s1 are equal to those of s2 Returns non-zero if first n letters are different
12
Exercise Using strncmp, implement the following function – Input – two strings str1, str2 Output – a pointer to the first instance of str2 in str1, or NULL Write a program that accepts two strings from the user and reports whether the first contains the second
13
solution strstr.c
14
Last one Implement the following function – Input – two strings str1, str2 Output – pointer to the first instance in str1 of any of the characters contained in Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces
15
solution strcspn.c
16
Some functions of string.h Implementations of two string.h functions: strcpy.c – copies one string into the other strchr.c – returns a pointer to the first occurrence of a character within a string strstr.c – returns a pointer to the first occurrence of one string within another strcspn.c – returns a pointer to the first occurrence of a single char from one string in the second
17
Dynamic Allocation Array variables have fixed size, used to store a fixed and known amount of variables This size can’t be changed after compilation However, we don’t always know in advance how much space we would need for an array or a variable We would like to be able to dynamically allocate memory
18
The malloc function void *malloc(unsigned int nBytes); The function malloc is used to dynamically allocate nBytes worth of space How to determine nBytes? malloc returns a pointer to the allocated area on success, NULL on failure You should always check whether memory was successfully allocated Remember to #include
19
Example dynamic_reverse_array.c
20
Why casting? The casting in y=(int *) malloc(n*sizeof (int)); is needed because malloc returns void * : void *malloc(unsigned int nbytes); The type void * specifies a general pointer, which can be cast to any pointer type.
21
What is this ‘sizeof’ ? The sizeof operator gets a variable or a type as an input and outputs its size in bytes: double x; s1=sizeof(x); /* s1 is 8 */ s2=sizeof(int) /* s2 is 4 */
22
Free the allocated memory segment void free(void *ptr); We use free(p) to free the allocated memory pointed to by p If p doesn’t point to an area allocated by malloc, a run-time error occurs Always remember to free the allocated memory once you don’t need it anymore Otherwise, you may run out of memory before you know it!
23
Another example another_strcpy.c
24
Exercise Implement the function my_strcat – Input – two strings, s1 and s2 Output – a pointer to a dynamically allocated concatenation (‘shirshur’) For example: The concatenation of “hello_” and “world!” is the string “hello_world!” Write a program that accepts two strings from the user and prints their concatenation Assume input strings are no longer than a 100 chars
25
Solution my_strcat.c (my_strcat2.c)
26
What’s wrong with this? char *my_strcat(char *str1, char *str2) { int len; char result[500]; /* Let’s assume this is large enough */ len = strlen(str1); strcpy(result, str1); strcpy(result+len, str2); return result; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.