320 likes | 479 Views
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 ‘’
E N D
APS105 Strings
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” • “”
Strings and Characters • Mind the difference between strings & chars • “B” • ‘B’ .
Accessing chars in a string • count the number of blanks in string s .
Reading Strings: scanf • using scanf: • skips leading whitespace • reads characters until finds more whitespace • terminates the string (adds ‘\0’ to the end) • Example: .
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)
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); .
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: .
Implementing strlen with only ptrs . Memory p q
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!
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: .
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: .
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
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
2D Array of Characters • Example: create an array of the months .
Array of Strings • Example: create an array of the months
Array of Strings: alternative • Example: create an array of the months .
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
Example • given an executable called myprogram: myprog input true 25 • then argc and argv are pre-set as follows: . argv
Ensuring Right Number of Args • ensure that myprogram has right num of args: myprog input true 25 .