Professional Documents
Culture Documents
1 !
! string input functions do not check whether sufcient space is allocated for entry!
2 !
/*write function to print string one char at time */! #include <stdio.h>! void printCharacters(const char *);! void main(void) {! char string[ ] = hi;! printCharacters(string);! putchar('\n');! }! /* Note: function printCharacters !
! returns nothing, ! ! takes as argument non-const pointer (can change) to constant data (not to be changed)! ! data const to protect from accidental change (just want to print) */!
3 !
/* print string one char at time w/ non-const ptr to const data */! #include <stdio.h>! void printCharacters(const char *);! void main(void) {! char string[ ] = hi;! printCharacters(string);! putchar('\n');! }! void printCharacters(const char *s) {! for (; *s!= '\0'; s++)! putchar(*s);! }! output: hi!
5 !
/* Copy string using array notation and pointer notation */! #include <stdio.h>! void copy1(char *, const char *);! void copy2(char *, const char *);! void main(void) {! char string1[10], *string2 = "Hi",! string3[10], string4[]= "Ho";! ! copy1(string1, string2);! printf("string1 = %s\n", string1);! copy2(string3, string4);! printf("string3 = %s\n", string3);! }!
6 !
/* Notes:!
to string not const, from string data const! - loop using pointer notation! ! no initialization! ! copying is done in continuation test! ! data modication: increment both to and from pointers to point to next char */!
8 !
/* Copy string using array notation and pointer notation */! #include <stdio.h>! void copy1(char *, const char *);! void copy2(char *, const char *);! void main(void) {! char string1[10], *string2 = "Hi",! string3[10], string4[]= "Ho";! copy1(string1, string2);! printf("string1 = %s\n", string1);! copy2(string3, string4);! printf("string3 = %s\n", string3);! }!
! Output: !
9 !
/* Program to compare two strings */! #include <stdio.h>! int str_compare(const char *, const char *);! void main(void) {! char! *string1 = "Hi",! *string2 = "Hi!";! printf("\"%s\" and \"%s\" are ", string1, string2);! if (!str_compare(string1, string2))! printf("identical\n");! else ! printf("different\n");! }!
10 !
int str_compare(const char *s1, const char *s2) {! for (; *s1 == *s2; s1++, s2++) ! if (*s1 == '\0')! return 0; /* strings are the same */! return 1; /* strings are different */! }! /* Notes: strings being compared, both contents protected by const!
! pointers not const - pointer incrementation used to move thru string! ! for loop ! ! condition: continue while what both pointers point to are same.! ! loop body: if reached end of one string (\0) - strings are same! ! data modication: increment both pointers to next char! ! if for loop exited w/o reaching end of string, then * s1 == * s2 was at some point not true, therefore, strings not equal. */! 11 !
/* Program to compare two strings */! #include <stdio.h>! int str_compare(const char *, const char *);! void main(void) {! char *string1 = "Hi, *string2 = "Hi!";! printf("\"%s\" and \"%s\" are ", string1, string2);! if (!str_compare(string1, string2))! printf("identical\n");! else ! printf("different\n");! }! Output: !
Hi and Hi! are different!
12 !
Summary so far!
! string print/compare/copy functions rely on the presence of \0 to work! ! 2 types of const or not with pointers:!
const datatype *const pointer_name;! ! const used to protect data that should not be overwritten! ! pointers that are incremented cannot be const!
! strings that are written into (copy to) need to have sufcient memory allocated!
13 !
Recall...!
! In memory,!
! ! characters are stored as one-byte ints! ! ! one-byte ints stored as binary #s! e.g., using lookup table to nd ASCII equiv of char ! a = 97 = 0110 0001!
15 !
17 !
Strings containing numbers not the same as numbers! ! e.g., string constant 2593 is not stored as 2 5 9 3 in discrete spaces!
2593 stored as 2 5 9 3 \0 char ascii bin rep 2 50 0011 0010 5 53 0011 0101 9 57 0011 1001 3 51 0011 0011 \0 0 0000 0000 discrete ints 2, 5, 9, 3 2 = 0000 0010 5 = 0000 0101 9 = 0000 1001 3 = 000 0011
19 !
21 !
!sprintf(sentence, These chars are written into sentence, can also have format specs);23 !!