1 / 31

APS105

APS105. Strings. C String storage. We have used strings in printf format strings Ex: printf(“Hello world<br>”); “Hello world<br>” is a string (of characters) C stores a string as an array of char’ s The string is terminated with a NULL character Null character is written ‘’

manton
Download Presentation

APS105

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. APS105 Strings

  2. C String storage • We have used strings in printf format strings • Ex: printf(“Hello world\n”); • “Hello world\n” is a string (of characters) • C stores a string as an array of char’s • The string is terminated with a NULL character • Null character is written ‘\0’ • Is actually represented by zero • (the world’s most expensive one-byte mistake!) • Example: • “hello” • “”

  3. Strings and Characters • Mind the difference between strings & chars • “B” • ‘B’ .

  4. Declaring Strings .

  5. char Arrays vs char Pointers . p

  6. Accessing chars in a string • count the number of blanks in string s .

  7. Printing a String .

  8. Reading Strings: scanf • using scanf: • skips leading whitespace • reads characters until finds more whitespace • terminates the string (adds ‘\0’ to the end) • Example: .

  9. Reading Strings: gets • using gets: void gets(char *s); • does not skip whitespace • reads until end of line • terminates the string (adds ‘\0’ to the end) • using getchar: char getchar(); • returns the next char from the input • whatever it might be (space, newline, etc)

  10. Example: use getchar to read string .

  11. Printing Strings & Pointer Math char *p = “Sample”; printf(“%s\n”,p); . printf(“%s\n”,p+2); . printf(“%c\n”,*p); . printf(“%c\n”,*(p+2)); . printf(“%c\n”,*p+2); .

  12. String Library Functions

  13. String Library and strlen #include <string.h> unsigned strlen(const char *p); • returns the length of the string pointed to by p • i.e., the number of characters up to ‘\0’ • Examples: .

  14. Implementing strlen .

  15. Implementing strlen with only ptrs . Memory p q

  16. Copying a String #include <string.h> char *strcpy(char *dst,const char *src); • src means source • dst means destination • copies the string (characters) from src to dst • stops copying when it finds ‘\0’ in src • returns dst (pointer to the destination string) • return value is usually ignored/discarded • warning: ensure that dst string is big enough! • ensure strlen(dst) >= strlen(src) or else big trouble!

  17. Implementing strcpy .

  18. Implementing strcpy .

  19. Concatenating Strings • Concatenate means glue together char *strcat(char *dst,const char *src); • writes src onto the end of dst • the ‘\0’ in dst is overwritten • the return value from strcat is usually discarded • Examples: .

  20. Comparing Strings intstrcmp(const char *p,const char *q); • compares p to q • returns: • zero if p and q are identical • a negative value if p is lexicographically before q • a positive value if q is lexicographically before p • lexicographically before: • “a” is before “b”, “A” is before “A”, “ “ is before all letters • “the” is before “them” • Example: .

  21. Other Usful String Functions char *strchr(const char *s, int c) • returns ptr to first appearance of c in s • returns NULL if not found • NOTE: “int c” (C will cast for you) char *strstr(const char *s1, const char *s2) • returns ptr to first appearance of s2 in s1 • returns NULL if not found

  22. Other Usful String Functions #include <stdlib.h> intatoi(const char *s) • converts leading characters of s into an integer • behaviour is undefined if no valid characters double atof(const char *s) • converts leading characters of s into a double • behaviour is undefined if no valid characters

  23. Arrays of Strings

  24. 2D Array of Characters • Example: create an array of the months .

  25. Array of Strings • Example: create an array of the months

  26. Array of Strings: alternative • Example: create an array of the months .

  27. Command Line Arguments

  28. Command Line Arguments • Main is a function; can pass it arguments • the arguments are entered on the command line • main can hence have one of these two formats: int main(void) int main(intargc, char *argv[]) • argc: argument count • how many arguments are there • includes the executable name in the count! • argv: argument vector • an array of argument strings • Note: can only be strings! • char *argv[] means argv is an array of pointers • could also write it as a double-pointer: char **argv

  29. Example • given an executable called myprogram: myprog input true 25 • then argc and argv are pre-set as follows: . argv

  30. Ensuring Right Number of Args • ensure that myprogram has right num of args: myprog input true 25 .

  31. Give a Program that Prints its args .

More Related