array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Pointer arithmetic strcpy void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; }
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…)
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; }
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
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?
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
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; }
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
Solution strrchr.c
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
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
solution strstr.c
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
solution strcspn.c
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
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
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
Example dynamic_reverse_array.c
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.
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 */
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!
Another example another_strcpy.c
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
Solution my_strcat.c (my_strcat2.c)
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; }