Transcript Part 2

C-tutorial - Part II
Prakash Linga
[email protected]
4110 Upson
All about Strings!
• Array of characters (chars)
• Definition: char *str;
• str is a pointer to a memory location which
stores a character (the first in the string).
• How we know the end of the string?
The character \0 (null character) indicates the
end of the string.
• Where on earth is the string stored?
Strings…
• In memory right!
• So allocate memory to store a string
char *str = (char *) malloc(sizeof(char)*MAXSTRLEN);
• How do I store the string now?
Brute-force: Store character by character.
Better Approach: Use strcpy
Note: What follows is mainly from SunOS man
pages.
Sssss
• strcpy
strcpy(char*destn, char* source);
#include <string.h>
char *str = (char *) malloc(sizeof(char)*MAXSTRLEN);
strcpy(str, “That was easy”);
• What is the length of my string?
Use strlen:
int len = strlen(str);
Strings in Java
• Strings are Objects
• Creating a String is a call to one of the constructors.
String str = new String(“This is ridiculously easy! Why can’t I do this in
C?”);
• Host of methods in the String class:
length, trim, compareTo, substring, charAt, concat,
indexOf , endsWith, replace
to name a few.
Back to the Obscure World!
• We have a host of functions for string
manipulation in Ctoo:
strcpy, strncpy, strlcpy
strcat, strncat, strlcat
strcmp, strncmp, strcasecmp, strncasecmp,
strchr, strrchr, strstr, strlen
strtok, strtok_r, strcspn, strspn, strdup, strpbrk
Copies
char *strcpy(char *s1, const char *s2);
char *strncpy(char *s1, const char *s2, size_t n);
size_t strlcpy(char *dst, const char *src, size_t dstsize);
strncpy copies over n bytes from the source to the destination
(many questions here!!!???)
strlcpy copies at most dstsize-1 characters from src to dst,
truncating src if necessary. Result is always null terminated.
(again many small questions!)
Cats
• Cats are similar to copies!
char *strcat(char *s1, const char *s2);
char *strncat(char *s1, const char *s2, size_t n);
size_t strlcat(char *dst, const char *src, size_t dstsize);
Any interesting questions here?
strncat: result is \0 terminated (always). This is not the case
with strncpy
Compares
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
strcasecmp and strncasecmp are case insensitive versions of
strcmp.
Return values for strcmp:
Returns 0 if equal; 1 if s1 > s2 and –1 otherwise.
Note: Use #include<strings.h> for strcasecmp and strncasecmp
strchr, strrchr and strstr
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
char *strstr(const char *s1, const char *s2);
strchr
Return a pointer to the first occurrence of c in s
strrchr
Return a pointer to the last occurrence of c in s
strstr
Return a pointer to the first occurrence of string s2 in s1
(any tricky case?)
Tok’s
char *strtok(char *s1, const char *s2);
char *strtok_r(char *s1, const char *s2, char **lasts);
The strtok() function can be used to break the string pointed to by s1 into a sequence of
tokens, each of which is delimited by one or more characters from the string pointed to by
s2.
The first call (with pointer s1 specified) returns a pointer to the first character of the first
token, and will have written a null character into s1 immediately following the returned
token.
The function keeps track of its position in the string between separate calls, so that
subsequent calls (which must be made with the first argument being a null pointer) will
work through the string s1 immediately following that token.
Strtok example
#include <string.h>
int main () {
char str[100];
char * tmp;
strcpy(str, "This is a sample string,just testing.");
printf ("Splitting \"%s\" into tokens:\n",str);
tmp = strtok (str," ,.");
while (tmp != NULL) {
printf ("%s\n",tmp);
tmp = strtok (NULL, " ,.");
}
return 0;
}
Output of tok program
Splitting "This is a sample string,just testing." into tokens:
This
is
a
sample
string
just
testing
Same example with strtok_r
#include <string.h>
int main () {
char str[100];
char * tmp, *last;
strcpy(str, "This is a sample string,just testing.");
printf ("Splitting \"%s\" into tokens:\n",str);
tmp = strtok_r (str," ,. " , &last);
while (tmp != NULL) {
printf ("%s\n",tmp);
tmp = strtok_r (NULL, " ,. " , &last);
}
return 0;
}
strspn and strcspn
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
The strcspn() function returns the length of the initial
segment of string s1 that consists entirely of characters
not from string s2.
The strspn() function returns the length of the initial segment
of string s1 that consists entirely of characters from string
s2.
strdup
char *strdup(const char *s1);
Make a copy (allocates memory using malloc) of the
parameter string and return a pointer to this copy
char *strpbrk(const char *s1, const char *s2);
The strpbrk() function returns a pointer to the first occurrence in
string s1 of any character from string s2, or a null pointer if no character
from s2 exists in s1.