aps105 strings. c string storage we have used strings in printf format strings –ex:...
TRANSCRIPT
![Page 1: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/1.jpg)
APS105
Strings
![Page 2: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/2.jpg)
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”– “”
![Page 3: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/3.jpg)
Strings and Characters
• Mind the difference between strings & chars• “B”
• ‘B’
.
![Page 4: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/4.jpg)
Declaring Strings
.
![Page 5: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/5.jpg)
char Arrays vs char Pointers.
p
![Page 6: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/6.jpg)
Accessing chars in a string
• count the number of blanks in string s.
![Page 7: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/7.jpg)
Printing a String
.
![Page 8: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/8.jpg)
Reading Strings: scanf
• using scanf:– skips leading whitespace– reads characters until finds more whitespace– terminates the string (adds ‘\0’ to the end)
• Example:.
![Page 9: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/9.jpg)
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)
![Page 10: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/10.jpg)
Example: use getchar to read string
.
![Page 11: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/11.jpg)
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); .
![Page 12: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/12.jpg)
String Library Functions
![Page 13: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/13.jpg)
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:
.
![Page 14: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/14.jpg)
Implementing strlen
.
![Page 15: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/15.jpg)
Implementing strlen with only ptrs
.
Addr Value
0
4
9
10
11
12
13
14
15
Memory
p
q
![Page 16: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/16.jpg)
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!
![Page 17: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/17.jpg)
Implementing strcpy
.
![Page 18: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/18.jpg)
Implementing strcpy
.
![Page 19: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/19.jpg)
Concatenating Strings
• Concatenate means glue togetherchar *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:
.
![Page 20: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/20.jpg)
Comparing Strings
int strcmp(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:
.
![Page 21: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/21.jpg)
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
![Page 22: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/22.jpg)
Other Usful String Functions
#include <stdlib.h>int atoi(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
![Page 23: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/23.jpg)
Arrays of Strings
![Page 24: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/24.jpg)
2D Array of Characters
• Example: create an array of the months.
![Page 25: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/25.jpg)
Array of Strings
• Example: create an array of the months
![Page 26: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/26.jpg)
Array of Strings: alternative
• Example: create an array of the months.
![Page 27: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/27.jpg)
Command Line Arguments
![Page 28: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/28.jpg)
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(int argc, 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
![Page 29: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/29.jpg)
Example
• given an executable called myprogram:myprog input true 25
• then argc and argv are pre-set as follows:.
argv
![Page 30: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/30.jpg)
Ensuring Right Number of Args
• ensure that myprogram has right num of args:myprog input true 25.
![Page 31: 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)](https://reader036.vdocuments.net/reader036/viewer/2022062408/56649f175503460f94c2e744/html5/thumbnails/31.jpg)
Give a Program that Prints its args
.