31
APS105 Strings

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)

Embed Size (px)

Citation preview

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

Give a Program that Prints its args

.