Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; 0 1 2 3 4 5 6 7 8 9 \0 The “\0” (null character)

Similar presentations


Presentation on theme: "Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; 0 1 2 3 4 5 6 7 8 9 \0 The “\0” (null character)"— Presentation transcript:

1 Strings CS240 Dick Steflik

2 What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character) in the 0 th position indicates the string is empty Remember that the name of an array is a pointer to it's first element; thisIsAString is the same as s where: char *s = thisIsAString;

3 scanf scanf is used for reading data from stdin according to some format specifier. For strings the format specifier is %s, scanf will read characters from stdin, up to the first blank and assign the string to the specified variable. scanf(“%s”,s); printf(“%d %s \n”,strlen(s),s); given the input from stdin “abc def ghi” the output will be: 3 abc The format specifier can have a modifier such as %12s, this indicated that the maximum number of characters to be read are 12, given the prev example the output will be the same, the read stops at the first blank

4 gets To read “whitespace” gets(b); printf("%d %s\n",strlen(b),b); given the input: “a b c d” the output will be: 7 a b c d Problem with gets is there is nothing to tell it when to stop, the same code above with the input: a b c d e f g h i j k l m will give the output: 25 a b c d e f g h i j k l m it does this by overflowing the array reserved for b and overwriting whatever happens to be defined next in memory. This is a classic “buffer overflow” Even though gets is useful it is “unsafe” and should be avoided

5 fgets fgets(targetstring,maxlength,filepointer) char b[5]; fgets(b,5,stdin); printf(“%d %s\n”,strlen(b),b); given the input: aaaaaaaaaaa the output is: 4 aaaa Successive fgets calls will keep reading from stdin as long as there is input: char a[10], b[10]; fgets(b,10,stdin); printf("%d %s\n",strlen(b),b); fgets(a,10,stdin); printf("%d %s\n",strlen(a),a); given the input: aaaaaaaaaabbbbbbbbbbcccccccccc the output is: 9 aaaaaaaaa 9 abbbbbbbb

6 strcpy char * strcpy(char * s1, char * s2); copies s2 into the char array s1, s1 is returned If s2 is longer than s1 then s1 will be overflowed, indicating that you must be careful using strcpy as it is unsafe; instead use strncpy. strncpy(dst, src, dst_size-1); dst[dst_size -1] = ‘\0’; /* just to be safe */ -or- /* allocate the destination buffer when you need it */ dst = (char *)malloc(strlen(src) +1); srtcpy(dst, src)

7 strcat (string concatenation) char * strcat(char * s1, char * s2) append string s2 onto the end of s1. Same problem as strcpy; if s2 is longer than the array defined for s1, the resulting string will overflow s1 and write over whatever happens to be defined in memory after s1. Better to use strncat as: strncat(dst, src, dst_size - strlen(dst) -1);

8 strchr & strrchr (search for a character) char * strchr(char * s, char c) search s for c return a pointer to first occurance, return null if not found. char * strrchr(char * s, char c) search s for c return a pointer to last occurance, return null if not found.

9 strcmp & strncmp (comparing strings) int strcmp(char * s1, char * s2) // compares s1 and s2 returns : less than 0 if s1 is lexically less than s2 0 if s1 and s2 are lexically equal greater than 1 if s1 is lexically greater than s2 int strncmp(char * s1, char * s2, int n) // compares first n characters of s1 and s2

10 strlen Int strlen(char * s1) returns the length of the specified character string

11 strcasecmp & strncasecmp Case insensitive versions of strcmp and strncmp; same return values as strcmp and strncmp

12 strstr char * strstr(char * s1, char * s2) find first occurrence of s2 in s1, return pointer to the occurrence strpbrk char * strpbrk(char * s1, char * s2) find the first occurrence of any character in s2 in s1, return a pointer to the occurrence or a null if no character in s2 is found in s1

13 strspn int strspn(char * s1, char * s2) returns the number of characters at the beginning of s1 that match s2 strcspn int strcspn(char * s1, char * s2) returns the number of characters at the beginning of s1 that do not match s2

14 strtok (string tokenizer) char * strtok(char * s1, char * s2) repeated calls to strtok breakes the string s1 into tokens that are seperated by the characters in s2), returns null if no more tokens. Each returned token will ne null (\0) terminated


Download ppt "Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; 0 1 2 3 4 5 6 7 8 9 \0 The “\0” (null character)"

Similar presentations


Ads by Google