2015年9月11日 星期五

[C] Pointers on C - note 2

/ **************************************************************************** /

1.

P. 243


The NUL terminates the string but is not considered a part of it, so the length of a string does not include the NUL. 


The header file string.h contains the prototypes and declarations needed to use the string functions. Although its use is not required, it is a good idea to include this header file because with the prototypes it contains the compiler can do a better job error checking your program. 


/ **************************************************************************** /

2.

p.246


char  message[] = "meg";
strcpy( mesaage, "s" );
   for(int i=0; i<4; i++)
    printf("%c", message[i]);
 
// real :  ['s'] [0] ['g'] [0] 


char  message[] = "meg";
 strcpy( mesaage, "send" ); 
    for(int i=0; i<4; i++)
    printf("%c", message[i]);
 
// Abort trap: 6  
//  Abort trap: 6 usually indicates a failed assertion.
 
 
/ ****************************************************************************/
 
3.
 
P. 247
 
  
char *strcpy( char *dst, char const *src ); 


To append (concatenate) one string to the end of another, strcat is used prototype is: 
 char *strcat( char *dst, char const *src );




strcmp returns a value less than zero if s1 is less than s2; a value greater than zero if s1 is greater than s2; and zero if the two strings are equal. 


int strcmp( char const *s1, char const *s2 ); 


 
/ **************************************************************************** /
  

4.

P.249


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 );
  
  
Like scrcpy, strncpy copies characters from the source string to the destination array. However, it always writes exactly len characters to dst. If strlen( src ) is less than len, then dst is padded to a length of len with additional NUL characters. If strlen( src ) is greater than or equal to len, then only len characters will be written to dst, and the result will not be NUL􏰊terminated! 



char  buffer[BSIZE];
      ...
      strncpy( buffer, name, BSIZE );
      buffer[BSIZE – 1] = '\0';
If the contents of name fit into buffer, the assignment has no effect. If name is too long, though, the assignment ensures that the string in buffer is properly terminated. Subsequent calls to strlen or other unrestricted string functions on this array will work properly.

 

5.

P.250


The easiest way to locate a specific character in a string is with the strchr and strrchr functions, whose prototypes are: 

            char  *strchr( char const *str, int ch );
            char  *strrchr( char const *str, int char );
 
Note that the second argument is an integer. It contains a character value, however. strchr searches the string str to find the first occurrence of the character ch. Then a pointer to this position is returned. If the character does not appear at all in the string, a NULL pointer is returned. strrchr works exactly the same except that it returns a pointer to the last (rightmost) occurrence of the character. 

Here is an example;
            char  string[20] = "Hello there, honey.";
            char  *ans;
ans = strchr( string, 'h' );
ans
will get the value string + 7 because the first 'h' appears in this position. Note
that case is significant.


6.

P.251


char *strpbrk( char const *str, char const *group ); 

This function returns a pointer to the first character in str that matches any of the characters in group, or NULL if none matched.
In the following code fragment,
            char  string[20] = "Hello there, honey.";
            char  *ans;
ans = strpbrk( string, "aeiou" );
ans
will get the value string + 1 because this position is the first that contains any of
the characters in the second argument. Once again, case is significant. 


7.

P.252


To locate a substring, strstr is used. Its prototype is:
            char  *strstr( char const *s1, char const *s2 );


This function finds the first place in s1 where the entire string s2 begins and returns a pointer to this location. If s2 does not appear in its entirety anywhere in s1, then NULL is returned. If the second argument is an empty string, then s1 is returned.


The standard library includes neither a strrstr nor a strrpbrk function, but they are easy to implement if you need them.

 This is an example of strrstr program:





#include <string.h>
char  *
my_strrstr( char const *s1, char const *s2 )
{
      register char     *last;
      register char     *current;
      /*
      ** Initialize pointer for the last match we've found.
      */
      last = NULL;
      /*
      ** Search only if the second string is not empty.  If s2 is
      ** empty, return NULL.
      */
      if( *s2 != '\0' ){
            /*
            ** Find the first place where s2 appears in s1.
            */
            current = strstr( s1, s2 );
            /*
            ** Each time we find the string, save the pointer to
            ** where it begins.  Then look after the string for
            ** another occurrence.
            */
            while( current != NULL ){
                  last = current;
                  current = strstr( last + 1, s2 );
} }
      /*
      ** Return pointer to the last occurrence we found.
      */
      return last;


}

  
8.

P.253

size_t strspn( char const *str, char const *group ); // 計算參數1需要經過多少個字元才會出現不屬於參數2中的字元。
size_t strcspn( char const *str, char const *group ); // 計算參數1需要經過多少個字元才會出現屬於參數2中的字元。
            int   len1, len2;
            char  buffer[] = "25,142,330,Smith,J,239-4123";
            len1 = strspn( buffer, "0123456789" );
            len2 = strspn( buffer, ",0123456789" );
 
            len3 = strcspn( buffer, ",330"); 
 
          // len1 = 2, len2 =11 , len3=2

     char *prt = buffer+strcspn(buffer, "4");
     printf("%ld\n", prt-buffer);
     // ans : 4

9.

P.253

'\r'是回車,'\n'是換行,前者使光標到行首,後者使光標下移一格。通常用的Enter是兩個加起來。下面轉一篇文章。


10.

P.254

string.h 的函數 strtok() ,需要兩個字串參數,以第二個參數字串的內容切割第一個參數字串。


char  *strtok( char *str, char const *sep );



11.

P.256


whitespace character : space '  ',
form feed
'\f',
newline
'\n',
carriage return lab
'\t',
or vertical tab
'\v'.






12.

P. 256
  1. The library includes two groups of functions that operate on individual characters, prototyped in the include file ctype.h. The first group is used in classifying characters, and the second group transforms them. 

    Example :




    isspace -  '  ', '\f' , '\t'  , '\n' , '\v'
    isdigit - a decimal digit 0 ~ 9 

    islower - a~z
    isupper - A~Z
    isalpha - a~z or A~Z

    ---------------------------------


     int   tolower( int ch );
     int   toupper( int ch );
    
    toupper returns the uppercase equivalent of its argument, and tolower returns the lowercase equivalent of its argument. If the argument to either function is not a character of the appropriate case, then it is returned unchanged.

沒有留言:

張貼留言