C-tutorial - Part II - PowerPoint PPT Presentation

About This Presentation
Title:

C-tutorial - Part II

Description:

C-tutorial - Part II Prakash Linga linga_at_cs.cornell.edu 4110 Upson – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 17
Provided by: p514
Category:
Tags: java | part | tutorial

less

Transcript and Presenter's Notes

Title: C-tutorial - Part II


1
C-tutorial - Part II
  • Prakash Linga
  • linga_at_cs.cornell.edu
  • 4110 Upson

2
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?

3
Strings
  • In memory right!
  • So allocate memory to store a string
  • char str (char ) malloc(sizeof(char)MAXSTRLE
    N)
  • 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.

4
Sssss
  • strcpy
  • strcpy(chardestn, char source)
  • include ltstring.hgt
  • char str (char ) malloc(sizeof(char)MAXSTRLE
    N)
  • strcpy(str, That was easy)
  • What is the length of my string?
  • Use strlen
  • int len strlen(str)

5
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 cant 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.

6
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

7
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!)

8
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

9
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 gt s2 and 1
    otherwise.
  • Note Use includeltstrings.hgt for strcasecmp and
    strncasecmp

10
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?)

11
Toks
  • 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.

12
Strtok example
  • include ltstring.hgt
  • int main ()
  • char str100
  • 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

13
Output of tok program
  • Splitting "This is a sample string,just testing."
    into tokens
  • This
  • is
  • a
  • sample
  • string
  • just
  • testing

14
Same example with strtok_r
  • include ltstring.hgt
  • int main ()
  • char str100
  • 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

15
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.

16
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.
Write a Comment
User Comments (0)
About PowerShow.com