Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

Η παρουσίαση φορτώνεται. Παρακαλείστε να περιμένετε

ΗΥ150 – ΠρογραμματισμόςΞ. Ζαμπούλης ΗΥ-150 Προγραμματισμός Αλφαριθμητικά (Strings)

Παρόμοιες παρουσιάσεις


Παρουσίαση με θέμα: "ΗΥ150 – ΠρογραμματισμόςΞ. Ζαμπούλης ΗΥ-150 Προγραμματισμός Αλφαριθμητικά (Strings)"— Μεταγράφημα παρουσίασης:

1 ΗΥ150 – ΠρογραμματισμόςΞ. Ζαμπούλης ΗΥ-150 Προγραμματισμός Αλφαριθμητικά (Strings)

2 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης Standard library accessed by #include 2

3 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 3 Χαρακτήρες Αναπαριστώνται από έναν δυαδικό αριθμό 8 δυαδικών ψηφίων (8 bits) (256 διαφορετικές τιμές) Η κωδικοποίησή τους έχει τυποποιηθεί με τον κώδικα ASCII Εκτός από γράμματα και ψηφία υπάρχουν και πολλοί ειδικοί χαρακτήρες Σταθερές χαρακτήρων – Η έκφραση 'z' είναι μια σταθερά και αντιστοιχεί σε έναν int με τα 8 τελευταία του bits ίσα με τον ASCII κωδικό του χαρακτήρα – 'z‘, ‘\t‘, ‘\n‘, κτλ

4 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 4 Βασικά για τα Αλφαριθμητικά (strings) Μια σειρά από χαρακτήρες που αντιμετωπίζονται σαν ένα αντικείμενο – Γράμματα, αριθμοί, ειδικοί χαρακτήρες (*, /, $) και όλοι οι εκτυπώσιμοι χαρακτήρες – Τιμές εισάγονται μέσα σε διπλά εισαγωγικά "Hello" – Τα Strings είναι πάντα πίνακες από χαρακτήρες Ένα String είναι δείκτης στον 1 ο χαρακτήρα του πίνακα Τιμή του string είναι η διεύθυνση του 1 ου χαρακτήρα του πίνακα

5 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 5 char vs. C string ‘A’ has data type char and is stored in 1 byte “A” is a C string of 2 characters and is stored in 2 bytes 5000 ‘A’ 6000 ‘A’ 6001 ‘\0’

6 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 6  There is a difference between 'A' and "A". The first one is character A and the second is string A.  Since strings are null terminated, "A" represents two characters, 'A' and '\0'.  "Hello" represents six characters, 'H', 'e', 'l', 'l', 'o', and '\0'.  To store 'A' we need only one memory cell of the type char, while to store "A", we need two memory cells of the type char, one for 'A' and the other for '\0'.  To store the string "HELLO" in computer we need six memory cells of the type char.  Consider the statement. char name[16];  Since C strings are null terminated and name has sixteen components, the largest string that can be stored in name is 15.  If you store a string of length, say 10 in name, the first 11 components of name are used and the last 5 are left unused.

7 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 7 Character Arrays strlen("hello, world"); /* string constant */ strlen(array); /* char array[100]; */ strlen(ptr); /* char *ptr; */ char pmessage[] = "now is the time"; /* an array */ char *amessage = "now is the time"; /* a pointer */

8 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 8 char message[8]; // declaration allocates memory To the compiler, the value of the identifier message alone is the base address of the array. We say message is a pointer (because its value is an address). It “points” to a memory location. message [0] [1] [2] [3] [4] [5] [6] [7] ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’ 6000

9 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 9 Ποιά η σχέση με τη malloc? Η malloc δεσμεύει στατικά ποσά μνήμης και επιστρέφει τον pointer σε αυτά. Έτσι χρειαζόμαστε την ειδική εντολή (realloc) για να τα αυξομειώσουμε τα ποσά αυτά, – αφού τελικά όλα πρέπει να τα «εγκρίνει» το λειτουργικό σύστημα.

10 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 10 Δηλώσεις Δηλώσεις αλφαριθμητικών – Σαν πίνακας από χαρακτήρες ή σαν δείκτης σε χαρακτήρα char * char color[] = "blue"; char *colorPtr = "blue"; – Κάθε string τελειώνει με '\0' και πρέπει να το λαμβάνουμε υπόψη στη δήλωση του πίνακα color has 5 elements

11 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 11 string[0] = ‘J’ string[1] = ‘I’ string[2] = ‘b’ string[3] = ‘r’ string[4] = ‘a’ string[5] = ‘n’ string[6] = ’ ’ string[7] = ‘B’ string[8] = ‘h’ string[9] = ‘a’ string[10] = ‘t’ string[11] = ‘\0’

12 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 12 Ανάγνωση Διάβασμα strings – Χρήση scanf scanf("%s", word); Αντιγράφει στο word[] Δεν χρειάζεται & (επειδή είναι και δείκτης) – Αφήνουμε χώρο στον πίνακα και για το '\0'

13 ΗΥ150 – ΠρογραμματισμόςΞ. Ζαμπούλης 1 2 3#include 4 5int main() 6{6{ 7 char string1[ 20 ], string2[] = "string literal"; 8 int i; 9 10 printf(" Enter a string: "); 11 scanf( "%s", string1 ); 12 printf( "string1 is: |%s|\nstring2: is |%s|\n“, string1, string2 ); 13 printf("string1 with spaces between characters is:\n“); 14 15 16 for ( i = 0; string1[ i ] != '\0'; i++ ) 17 printf( "%c ", string1[ i ] ); 18 19 printf( "\n" ); 20 return 0; 21} Enter a string: Hello there string1 is: Hello string2 is: string literal string1 with spaces between characters is: H e l l o

14 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 14 Παραδείγματα Πρόβλημα : Εκτυπώστε ανάστροφα το κείμενο που θα διαβαστεί από την οθόνη. #include #define N 100 void inversion(char *s) { int i; for (i = strlen(s)-1; i >= 0; --i) printf("%c",s[i]); printf(“\n”); } int main() { char s[N]; printf("Dwste mia le3h mexri %d xarakthres\n",N); scanf("%s",s); inversion(s); return 0; }

15 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 15 Διαχείριση Αλφαριθμητικών Βρίσκονται στο Μετατρέπουν αλφαριθμητικά (αν είναι κατάλληλα) σε αριθμητικές τιμές

16 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 16 Συναρτήσεις Ανάγνωσης και Εκτύπωσης Βρίσκονται στο int sprintf(char *s, const char *format, …) Ισοδύναμη με την printf μόνο π ου η έξοδος είναι στο string s και όχι στην οθόνη int sscanf(char *s, const char *format, …) Ισοδύναμη με την scanf μόνο π ου η είσοδος είναι α π ό το string s και όχι α π ό το π ληκτρολόγιο char s[100]; char f[] = "1.45 2.2 0.12"; float t1,t2,t3; sprintf(s,"%s",f); sscanf(s,"%f %f %f",&t1,&t2,&t3); printf("s = %s\nt1 = %f t2 = %f t3 = %f\n",s,t1,t2,t3);

17 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 17 Χρήσιμες Συναρτήσεις (string.h) size_t strlen( const char *s ); – Επιστρέφει τον αριθμό των χαρακτήρων πριν το ‘\0’ που βρίσκονται στο s (το μήκος string) char *strdup(const char *s1); – Δεσμεύει όση μνήμη χρειάζεται και αντιγράφει σε αυτήν το αλφαριθμητικό στο s1. Επιστρέφει την καινούργια μνήμη με το καινούργιο αντίγραφο του s1. Η δεσμευμένη μνήμη χρειάζεται να αποδεσμευτεί στο τέλος με την free.

18 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 18 strlen /* strlen: return length of string s */ int strlen(char *s) { int n; for (n = 0; s[n] != '\0', n++) ; return n; }

19 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 19 strlen /* strlen: return length of string s */ int strlen(char *s) { char *p = s; while (*p != '\0') p++; return p − s; }

20 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 20 strlen /* strlen: return length of string s */ int strlen(char *s) { int n; for (n = 0; *s != '\0', s++) n++; return n; }

21 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 21 strcpy /* strcpy: copy t to s; array subscript version */ void strcpy(char *s, char *t) { int i; i = 0; while ((s[i] = t[i]) != '\0') i++; }

22 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 22 strcpy /* strcpy: copy t to s; pointer version */ void strcpy(char *s, char *t) { while ((*s = *t) != '\0') { s++; t++; }

23 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 23 Συναρτήσεις σύγκρισης (string.h) Σύγκριση αλφαριθμητικών – Συγκρίνονται οι ASCII κώδικες των χαρακτήρων int strcmp( const char *s1, const char *s2 ); – Συγκρίνει το s1 με το s2 – Επιστρέφει: αρνητικό αριθμό αν s1 < s2, μηδέν αν s1 == s2, ή θετικό αν s1 > s2

24 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 24 Συναρτήσεις σύγκρισης (string.h) Σύγκριση αλφαριθμητικών – Συγκρίνονται οι ASCII κώδικες των χαρακτήρων int strcmp( const char *s1, const char *s2 ); – Συγκρίνει το s1 με το s2 – Επιστρέφει: αρνητικό αριθμό αν s1 < s2, μηδέν αν s1 == s2, ή θετικό αν s1 > s2 Αλφαριθμητική σειρά, με βάση το ASCII. Κάθε ψηφίο/χαραρακτήρας του string είναι ένα ψηφίο του αριθμού/αλφαρηθμητικού στο 256-δικό σύστημα.

25 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 25 strcmp /* strcmp: return 0 if s>t */ int strcmp(char *s, char *t) { int i; for (i = 0; s[i] == t[i]; i++) if (s[i] == '\0') return 0; return s[i] − t[i]; }

26 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 26 strcmp /* strcmp: return 0 if s>t */ int strcmp(char *s, char *t) { for ( ; *s == *t; s++, t++) if (*s == '\0') return 0; return *s − *t; }

27 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 27 char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; if ( myName == yourName ) // compares addresses only! { // That is, 4000 and 6000 here.. // DOES NOT COMPARE CONTENTS!. } myName [0] ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’... yourName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’... 4000 6000

28 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 28 char myName [ 21 ] = “Huang” ; char yourName [ 21 ] ; strcpy ( yourName, myName ) ; // changes string yourName // OVERWRITES CONTENTS! myName [0] ‘H’ ‘u’ ‘a’ ‘n’ ‘g’ ‘\0’... yourName [0] ‘H’ ‘e’ ‘a’ ‘d’ ‘i‘ ‘n’ ‘ g’ ‘t’ ‘o’ ‘n’ ‘\0’... 4000 6000 ‘u’ ‘n’ ‘g’ ‘\0’

29 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 29 String literals Evaluating ″ dog ″ results in memory allocated for three characters ′ d ′, ′ o ′, ′ g ′, plus terminating NUL char *m = ″ dog ″ ; Note: If m is an array name, subtle difference: char m[10] = ″ dog ″ ; 10 bytes are allocated for this array This is not a string literal; It’s an array initializer in disguise! Equivalent to { ′ d ′, ′ o ′, ′ g ′, ′ \0 ′ }

30 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 30 String manipulation functions Read some “source” string(s), possibly write to some “destination” location char *strcpy(char *dst, char const *src); char *strcat (char *dst, char const *src); Programmer’s responsibility to ensure that: – destination region large enough to hold result – source, destination regions don’t overlap “undefined” behavior in this case – according to C spec, anything could happen! char m[10] = ″ dog ″ ; strcpy(m+1, m); Assuming that the implementation of strcpy starts copying left-to-right without checking for the presence of a terminating NUL first, what will happen?

31 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 31 strlen() and size_t size_t strlen(char const *string); /* returns length of string */ size_t is an unsigned integer type, used to define sizes of strings and (other) memory blocks – Reasonable to think of “size” as unsigned”... – But beware! Expressions involving strlen() may be unsigned (perhaps unexpectedly) if (strlen(x) – strlen(y) >= 0)... avoid by casting: ((int) (strlen(x) – strlen(y)) >= 0) – Problem: what if x or y is a very large string? a better alternative: (strlen(x) >= strlen(y)) always true!

32 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 32 strcmp() “string comparison” int strcmp(char const *s1, char const *s2); – returns a value less than zero if s1 precedes s2 in lexicographical order; – returns zero if s1 and s2 are equal; – returns a value greater than zero if s1 follows s2. Source of a common mistake: – seems reasonable to assume that strcmp returns “true” (nonzero) if s1 and s2 are equal; “false” (zero) otherwise – In fact, exactly the opposite is the case!

33 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 33 Restricted vs. unrestricted string functions Restricted versions: require an extra integer argument that bounds the operation char *strncpy(char *dst, char const *src, size_t len); char *strncat(char *dst, char const *src, size_t len); int strncmp(char const *s1, char const *s2, size_t len); – “safer” in that they avoid problems with missing NUL terminators – safety concern with strncpy : If bound isn’t large enough, terminating NUL won’t be written Safe alternative: strncpy(buffer, name, BSIZE); buffer[BSIZE-1] = ′ \0 ′ ; - SUBSTRINGS

34 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 34 String searching char *strpbrk(char const *str, char const *group); /* return a pointer to the first character in str that matches *any* character in group; return NULL if there is no match */ size_t *strspn(char const *str, char const *group); /* return number of characters at beginning of str that match *any* character in group */ Ο pointer δείχνει «μέσα» στο αλφαρηθμητικό. Χρειαζόμαστε ωστόσο να, θυμόμαστε, με άλλο pointer, και την αρχή του αλφαρηθμητικού.

35 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 35 strtok “string tokenizer” char *strtok(char *s, char const *delim); /* delim contains all possible ″ tokens ″ : characters that separate ″ tokens ″. if delim non-NULL: return ptr to beginning of first token in s, and terminate token with NUL. if delim is NULL: use remainder of untokenized string from the last call to strtok */

36 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 36 strtok in action for (token = strtok(line, whitespace); token != NULL; token = strtok(NULL, whitespace)) printf( ″ Next token is %s\n ″, token); line dogcatNUL token NUL

37 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 37 An implementation of strtok char* strtok(char *s, const char *delim) { static char *old = NULL; char *token; if (! s) { s = old; if (! s) return NULL; } if (s) { s += strspn(s, delim); if (*s == 0) { old = NULL; return NULL; } } token = s; s = strpbrk(s, delim); if (s == NULL) old = NULL; else { *s = 0; old = s + 1; } return token; } old contains the remains of an earlier s value (note use of static ) NULL has been passed in for s, so consult old strspn returns number of delimiters at beginning of s – skip past these characters strpbrk gives the position of the next delimiter. s is updated to this position, but token still points to the token to return.

38 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 38 main( ) { char name1[12],name2[12],mixed[25]; char title[20]; strcpy(name1,"Rosalinda"); strcpy(name2,"Zeke"); strcpy(title,"This is the title."); printf(" %s\n\n"title); printf("Name 1 is %s\n",name1); printf(Name 2 is %s\n",name2); if (strcmp(name1,name2)>0) /* return 1 if name1 > name2 */ strcpy(mixed,name1); else strcpy(mixed,name2); printf("The biggest name alphabetically is %s\n",mixed); strcpy(mixed,name1); strcat(mixed," "); strcat(mixed,name2); printf("Both names are %s\n",mixed); }

39 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 39 Array of strings

40 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 40 The strchr function char *strchr(const char *s, int c); The strchr() function shall locate the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null byte is considered to be part of the string. The function returns the location of the found character, or a null pointer if the character was not found. #include /* strchr */ char *(strchr)(const char *s, int c) { /* Scan s for the character. When this loop is finished, s will either point to the end of the string or the character we were looking for. */ while (*s != '\0' && *s != (char)c) s++; return ( (*s == c) ? (char *) s : NULL ); }

41 ΗΥ150 – Προγραμματισμός Ξ. Ζαμπούλης 41 The strstr function char *strstr(const char *haystack, const char *needle); The strstr() function shall locate the first occurrence in the string pointed to by haystack of the sequence of bytes (excluding the terminating null byte) in the string pointed to by needle. The function returns the pointer to the matching string in haystack or a null pointer if a match is not found. If needle is an empty string, the function returns haystack. #include /* strstr */ char *(strstr)(const char *haystack, const char *needle) { size_t needlelen; /* Check for the null needle case. */ if (*needle = = '\0') return (char *) haystack; needlelen = strlen(needle); for (; (haystack = strchr(haystack, *needle)) != NULL; haystack++) if (strncmp(haystack, needle, needlelen) == 0) return (char *) haystack; return NULL; }


Κατέβασμα ppt "ΗΥ150 – ΠρογραμματισμόςΞ. Ζαμπούλης ΗΥ-150 Προγραμματισμός Αλφαριθμητικά (Strings)"

Παρόμοιες παρουσιάσεις


Διαφημίσεις Google