Post on 06-Apr-2018
8/3/2019 P9 Strings
1/21
8/3/2019 P9 Strings
2/21
STRINGSSTRINGS
Array of characters
Arranged one after another in memory
Null end of string Enclosed within double quotes
Example:
hello worldaugust
8/3/2019 P9 Strings
3/21
STRINGSSTRINGS
Character-1 byte
Successive characters occupies successivebytes
Null ascii value zero
Null \0
8/3/2019 P9 Strings
4/21
STRINGSSTRINGS--DeclarationDeclaration
S-string of 5 characters
Square braces represents number of characters
Number of characters can be omitted
Number of characters 5 includes NULL
Char s[5];Data type
Name of array
Size of array
8/3/2019 P9 Strings
5/21
STRING HANDLINGSTRING HANDLING
Initialization of string
char s[5]=test;
Characters of the string-pair of double quoteschar s[5]={t,e,s,t};
Individual characters pair of single quotes
char s[]=test;
No of characters can be omitted.
8/3/2019 P9 Strings
6/21
STRING HANDLINGSTRING HANDLING
In printf, %s used to print the string on the
console.
EXAMPLE: Char s[6]=string;
Printf(%s,s);
Output would be:string.,..G..
8/3/2019 P9 Strings
7/21
STRING HANDLINGSTRING HANDLING
puts() function To print the string
Append newline character
Example:
Char s[7]=string;
Char t[5]=test;
Puts(s);
Puts(t);
Output would be as follows;
string
test
8/3/2019 P9 Strings
8/21
STRING HANDLINGSTRING HANDLING
Initializing at run time:
char s[5];
scanf(%s,&s);scanf(%s,s);
gets() function can also be used:
gets(s)
8/3/2019 P9 Strings
9/21
STRING HANDLINGSTRING HANDLING
Accessing individual characters
Char s[5]=test;
For(i=0;i
8/3/2019 P9 Strings
10/21
Review of stringsReview of strings
1010
Sequence of zero or more characters,terminated by NUL (literally, the integer value
0)
NUL terminates a string, but isnt part of it important forstrlen() length doesnt include
the NUL
Strings are accessed throughpointers/array names
string.h contains prototypes of many useful
functions
8/3/2019 P9 Strings
11/21
String literalsString literals
1111
Evaluating dog results in memoryallocated for three characters e , s , g
, plus terminating NULchar *m = esg;
Note: Ifm is an array name, subtle
difference:char m[10] = esg ;10 bytes are allocated for this array
This is not a string literal;
Its an array initializer in disguise!
Equivalent to{e,s,g,\0}
8/3/2019 P9 Strings
12/21
String manipulation functionsString manipulation functions
1212
Read some source string(s), possibly write to some destination locationchar *strcpy(char *dst, char const *src);
char *strcat (char *dst, char const *src);
Programmers responsibility to ensure that:
destination region large enough to hold result
source, destination regions dont overlap undefined behavior in this case
according to C spec, anything could happen!
char m[10] = esg ;
strcpy(m+1,m);
Assuming that the implementation of
strcpy starts copying left-to-right without
checking for the presence of a terminating
NUL first, what will happen?
8/3/2019 P9 Strings
13/21
strlen()strlen() andand size_tsize_t
1313
size_t strlen(char const *string);/* returns length of string */
` size_t is an unsigned integer type, used todefine sizes of strings and (other) memoryblocks
` Reasonable to think of size as unsigned...` But beware! Expressions involving strlen() may be
unsigned (perhaps unexpectedly)if (strlen(x) strlen(y) >= 0) ...
`avoid by casting:((int) (strlen(x) strlen(y)) >= 0)` Problem: what ifx ory is a very large string?
` a better alternative: (strlen(x) >= strlen(y))
always true!
8/3/2019 P9 Strings
14/21
strcmpstrcmp()() string comparisonstring comparison
1414
int strcmp(char const *s1, char const *s2);
returns a value less than zero ifs1 precedes
s2 in lexicographical order;
returns zero ifs1 and s2 are equal;
returns a value greater than zero ifs1 follows
s2.
strcmp returns true (zero) ifs1 and s2 are
equal; false (nonzero) otherwise
8/3/2019 P9 Strings
15/21
Restricted vs. unrestricted stringRestricted vs. unrestricted string
functionsfunctions
1515
Restricted versions: require an extra integer argument that bounds theoperation
char *strncpy(char *dst, char const *src, size_t len);
char *strncat(char *dst, char const *src, size_t len);
int strncmp(char const *s1, char const *s2, size_t len);
safer in that they avoid problems with missing NUL terminators
safety concern with strncpy:
If bound isnt large enough, terminating NUL wont be written
Safe alternative:
strncpy(buffer, name, BSIZE);
buffer[BSIZE-1] = \0;
8/3/2019 P9 Strings
16/21
String searchingString searching
1616
char *strpbrk(char const *str, char const *group);/* return a pointer to the first character in str
that matches *any* character in group;
return NULL if there is no match */
size_t *strspn(char const *str, char const *group);
/* return number of characters at beginning of str
that match *any* character in group */
8/3/2019 P9 Strings
17/21
strtokstrtok string tokenizerstring tokenizer
1717
char *strtok(char *s, char const *delim);/* delim contains all possible tokens:
characters that separate tokens.
ifdelim non-NULL:
return ptr to beginning offirst token in s,
and terminate token with NUL.
ifdelim is NULL:
use remainder of untokenized string from the
last call to strtok */
8/3/2019 P9 Strings
18/21
STRING HANDLINGSTRING HANDLING
String functions defined in string.h
Strcat-appends one string to other
Strcmp-compare two strings
Strcpy-copy one string to other
Strlen-returns length of a string
etc.
8/3/2019 P9 Strings
19/21
POINTER TO STRINGPOINTER TO STRING
Similar to integer arrays
Char name[]=string;
Char *p; P=name;/*base address is assigned to ptr*/
Then,ptr is incremented to point to the next
character. Process is carried out till ptr doesnt point to
the last character \0.
8/3/2019 P9 Strings
20/21
I request Electronics and communication
ENGINEERING students to visit my blogfor
more
abhishek1ek.blogspot.com
awhengineering.blogspot.com
8/3/2019 P9 Strings
21/21