1 / 28

C Characters & Strings

C Characters & Strings. Character Review Character Handling Library Initialization Standard Input/Output Library Functions String Conversion Functions String Handling Library. Character Review. Know your ASCII '0' 48 dec 0x30 0011 0000 '9' 57 dec 0x39 0011 1001

cullen
Télécharger la présentation

C Characters & Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Characters & Strings • Character Review • Character Handling Library • Initialization • Standard Input/Output Library Functions • String Conversion Functions • String Handling Library

  2. Character Review • Know your ASCII • '0' 48 dec 0x30 0011 0000 • '9' 57 dec 0x39 0011 1001 • 'A' 65 dec 0x41 0100 0001 • 'Z' 90 dec 0x5A 0101 1010 • 'a' 97 dec 0x61 0110 0001 • 'z' 122 dec 0x7A 0111 1010

  3. Character Handling Library • Use #include <ctype.h> header file • Basic functions include: • Conversion between case • Test for upper or lower case • Test for letters (and digits and alphanumeric) • Test for control characters • Test for whitespace (and punctuation) • See example at char.c

  4. Some Functions • int islower(char) • Is it lowercase letter? • int isupper(char) • Is it uppercase letter? • int tolower(char) • Convert to lowercase • int toupper(char) • Convert to uppercase • int iscntrl(char) • Is it a control character? (tab, alarm, backspace, newline, etc.)

  5. More Functions • int isalpha(char) • Is it a letter? • int isalnum(char) • Is it a letter or digit? • int isdigit(char) • Is it a digit? • int isspace(char) • Is it a space, tab, or newline? • int ispunct(char) • Is it printable but not space or alphanumeric?

  6. String Initialization • Store 6 characters + null character (null character = '\0' = 0x0) • char s[]="string"; • char s[7]="string"; • char s[]={'s','t','r','i','n','g','\0'}; • char s[7]={'s','t','r','i','n','g','\0'}; • char s[SIZE]="string"; • Where #define SIZE 7

  7. String Initialization • Stores one ‘a’ + null characters • char s[7]={'a'}; • Stores ‘a’ + ‘b’ + null characters • char s[7]={'a', 'b'}; • Stores 7 null characters • char s[7]={0}; • char s[7]={0x0}; • char s[7]={'\0'};

  8. String Initialization • Creates an array of pointers to two constant character strings (See init.c) char *c1[] = {"zero","one"}; printf("%s\n",c[0]); /* zero */ printf("%p\n",c[0]); /* 0x10970 */ printf("%p\n",&c[0]); /* 0xffbefa88 */ printf("%c\n",c[0][0]); /* z */ printf("%p\n",&c[0][0]); /* 0x10970*/ c[0][0]='a'; /* Segmentation fault */ c[0]=c[1]; /* assign another address */

  9. String Initialization • Creates an array of characters char c2[2][5] = {"zero", "one"}; printf("%s\n",c2[0]); /* zero */ printf("%p\n",c2[0]); /* 0xffbefa78 */ printf("%p\n",&c2[0]); /* 0xffbefa78 */ printf("%c\n",c2[0][0]); /* z */ printf("%p\n",&c2[0][0]); /* 0xffbefa78 */ c2[0][0]='a'; /* ok */ c2[0]=c2[1]; /* incompatible types */

  10. String Initialization • Creates an array of characters char c3[2][5] = {'z','e','r','o','\0', 'o','n','e','\0','\0'}; printf("%s\n",c3[0]); /* zero */ printf("%p\n",c3[0]); /* 0xffbefa68 */ printf("%p\n",&c3[0]); /* 0xffbefa68 */ printf("%c\n",c3[0][0]); /* z */ printf("%p\n",&c3[0][0]); /* 0xffbefa68 */ c3[0][0]='a'; /* ok */ c3[0]=c3[1]; /* incompatible types */

  11. String Conversion Functions • Convert from strings to integers and floats • Basic functions include: • String to integer, float • Cut off non-numerical data • Display base 2-36 • Requires the #include<stdlib.h> header • General utilities library

  12. String Conversion Functions • Converts string to an integer or double • int atoi(const char *nPtr); • double atof(const char *nPtr); • Examples int i=atoi("1234"); double d=atof("1.23"); int x=atoi('5'); /*What’s wrong with this one?*/

  13. What’s the Output? #include <stdio.h> #include <stdlib.h> int main(){ char a[]="15", b[]="+2.7"; int c=atoi(a), i=0; double d=atof(b); printf("%x\n",*a); printf("%c\n",*b); printf("%x\n",c); printf("%f\n",d); return 0; } //See exercise1.c

  14. String Conversion Functions • double strtod(const char *nPtr, char **endPtr); • Converts string nPtr to double • endPtr is assigned the 1st character after the converted value • char **ptr is for a pointer to an array of characters (also written as char *ptr[]) char *nextChar = NULL; char *str = "123abc "; double d=strtod(str, &nextChar); /* d=123.000000 nextChar="abc\0" */

  15. Standard I/O Library Functions • Use <stdio.h> library • int getchar(void); • Returns one character at a time from standard input • int putchar(int c); • Prints the character stored in “c” • char* puts(const char *s); • Prints the string followed by a newline character

  16. Standard I/O Library Functions • Use <stdio.h> library • char* gets(char *s); • Reads one line from standard input & puts it into array “s”. Includes blank spaces. • Should not be used because of the potential for “buffer overflow” attack • char* fgets(char* s, int n, FILE* stream); • Copies characters from (input) stream stream to s, stopping when n-1 characters copied, newline copied, end-of-file reached or error occurs • Ex: char str[10]; fgets(str, sizeof(str), stdin); printf(str);

  17. String Handling Library • Functions for manipulating strings • Requires the #include<string.h> header • Basic functions include: • Copying strings • Comparing strings • Searching strings • Tokenizing strings • Determining length of strings

  18. Copy, Append, Compare • char *strcpy(char *s1, const char *s2); • Copies s2 into s1; the value of s1 is returned • char *strcat(char *s1, const char *s2); • Appends s2 to s1; the value of s1 is returned • char *strcmp(char *s1, const char *s2); • s1 > s2 returns positive number • s1 == s2 returns 0 • s1 < s2 returns negative number

  19. Example Code char str[]="string"; char *ptr; strcpy(ptr, str); strcat(ptr, str); printf("%s\n",str); /*string*/ printf("%s\n",ptr); /*stringstring*/ printf("%d\n",strcmp(ptr,str)); /*115*/ printf("%d\n",strcmp(ptr,ptr)); /*0*/ printf("%d\n",strcmp(str,ptr)); /*-115*/ //See strcpy.c

  20. Search Functions • char *strchr(const char *s, int c); • Locates & returns a pointer to the 1st occurrence of c in string s • Returns NULL if not found • char *strstr(const char *s1, const char *s2); • Locates & returns a pointer to the 1st occurrence of string s2 in string s1 • Returns NULL if not found

  21. Example Code char str[]="string", rin[]="rin", abc[]="abc"; char i = 'i', z = 'z'; printf("%p\n",strchr(str,i)); /*0xffbefa8b*/ printf("%s\n",strchr(str,i)); /*ing*/ printf("%p\n",strchr(str,z)); /*0x0*/ printf("%p\n",strstr(str,rin)); /*0xffbefa8a*/ printf("%s\n",strstr(str,rin)); /*ring*/ printf("%p\n",strstr(str,abc)); /*0x0*/ //See strstr.c

  22. What’s the Output? #include<stdio.h> #include<string.h> main(){ char str1[]="apple", str2[]="banana"; char ptr1[20], *ptr2=str2; strcpy(ptr1, strchr(str1,'p')); strcat(ptr1, strstr(str2, "nan")); str2[0]='N'; printf("%s\n",ptr1); printf("%s\n",ptr2); } //See exercise2.c

  23. String Tokenizer • char *strtok(char *s1, const char *s2); • Used to break a string into tokens • A sequence of characters separated by delimiting characters (usually spaces or punctuation marks) • s1 contains string to be tokenized • s2 contains the characters used to separate the tokens

  24. String Tokenizer #include <stdio.h> #include <string.h> int main(){ char str[]="This is a string."; char *tPtr=strtok(str," "); /*searches for the 1st non-delimiting character (space) & inserts '\0' at next delimiter character*/ while(tPtr!=NULL){ printf("%s\n",tPtr); tPtr=strtok(NULL," "); /*keeps inserting '\0' in next delimiter*/ } return 0; }//See token.c

  25. String Tokenizer • Output from last slide This is a string. This • Problem with strtok is that it changes the original string

  26. What’s the Output? #include <stdio.h> #include <string.h> int main(){ char str[]="abrakadabra"; char *tPtr=strtok(str,"a"); while(tPtr!=NULL){ printf("%s",tPtr); tPtr=strtok(NULL,"a"); } printf("\n%s\n",str); return 0; } //See exercise3.c

  27. Length of a String • size_t strlen(const char s); • Returns the length of string s • Does not include the terminating null character

  28. Example Code #include<stdio.h> #include<string.h> main(){ char str1[]="apple", *str2="banana"; char str3[6]={'a','b','\0','c','d','\0'}; printf("%d\n",strlen(str1)); /* 5 */ printf("%d\n",strlen(str2)); /* 6 */ printf("%d\n",strlen(str3)); /* 2 */ } //See strlen.c

More Related