sundeepblue

Computer Graphics, CAGD, Demoscene, intro [crack each line of code, cram each bit of byte, create each idea of mind]

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
Manipulating Strings
--------------------------------------------------------------
size_t strlen(char *str)
---------------------------------------------------------------
char* strcpy(char* destination, char* source)
eg:
 
/*strcpy demo*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
char source[] = "The source string.";
int main()
{
 
char dest1[80];
char *dest2, *dest3;
 
strcpy(dest1, source);
 
/* To copy to dest2 you must allocate space*/
dest2 = (char *)malloc(strlen(source) + 1);
strcpy(dest2, source);
return 0;
 
}
 
---------------------------------------------------------------------
 char *strncpy(char *destination, char *source, size_t n)
 
In fact, strncpy() is similar to strcpy(), except that strncpy() lets you specify how many characters to copy.
---------------------------------------------------------------------
 char *strdup(char *source)
 
in fact, strdup() is similar to strcpy(), except that strdup() performs its own memory allocation for the destination string with a call to malloc().
---------------------------------------------------------------------
char *strcat(char *str1, char *str2)
The function appends a copy of str2 onto the end of str1, moving the terminating null character to the end of the new string. You must llocate enough space for str1 to hold the resulting string. The return value of strcat() is a pointer to str1.
 
---------------------------------------------------------------------
COMPARING STRINGS
 
Strings are compared to determine whether they are equal or unequal. If they are unequal, one string is "greater than" or "less than" the other. Determinations of "greater" and "less" are made with the ASCII codes of the characters. In the case of letters, this is equivalent to alphabetical order, with the one seemingly strange exception that all uppercase letters are "less than" the lowercase letters. This is true because the uppercase letters have ASCII codes 65 through 90 for A through Z, while lowercase a through z are represented by 97 through 122. Thus, "ZEBRA" would be considered to be less than  apple" by these C functions.

The ANSI C library contains functions for two types of string  omparisons: comparing two entire strings, and comparing a certain number of characters in two strings.
 
---------------------------------------------------------------------
int strcmp(char *str1, char *str2)
useage: compares two strings character by character.
 
---------------------------------------------------------------------
int strncmp(char *str1, char *str2, size_t n)
useage: compares a specified number of characters of one string to another string.
 
---------------------------------------------------------------------
char *strchr(char *str, int ch)
useage: finds the first occurrence of a specified character in a string.
 
---------------------------------------------------------------------
size_t strcspn(char *str1, char *str2)
useage: searches one string for the first occurrence of any of the characters in a second string.
 
The function strcspn() starts searching at the first character of str1, looking for any of the individual characters contained in str2. This is important to remember. The function doesn't look for the string str2, but only the characters it contains. If the function finds a match, it returns the offset from the beginning of str1, where the matching character is located. If it finds no match, strcspn() returns the value of strlen(str1). This indicates that the first match was the null character terminating the string
 
---------------------------------------------------------------------
char *strstr(char *str1, char *str2)
useage: perhaps most useful, C string-searching function is strstr(). This function searches for the first occurrence of one string within another, and it searches for the entire string, not for individual characters within the string.
 
The function strstr() returns a pointer to the first occurrence of str2 within str1. If it finds no match, the function returns NULL. If the length of str2 is 0, the function returns str1. When strstr() finds a match, you can obtain the offset of str2 within str1 by  ointer subtraction, as explained earlier for strchr(). The matching procedure that strstr() uses is case-sensitive.
 
---------------------------------------------------------------------
char *strrev(char *str)
useage: reverses the order of all the characters in a string (Not ANSI Standard). The order of all characters in str is reversed, with the terminating null character remaining at the end.
 
---------------------------------------------------------------------
STRING-TO-NUMBER CONVERSIONS
Three functions can be used to convert a string to a number.
They are in STDLIB.H
---------------------------------------------------------------------
int atoi(char *ptr);
useage: The function atoi() converts the string pointed to by ptr to an integer. Besides digits, the string can contain leading white space and a + or -- sign. Conversion starts at the beginning of the string and proceeds until an unconvertible character (for example, a letter or punctuation mark) is encountered. The resulting integer is returned to the calling program. If it finds no convertible  characters, atoi() returns 0. Table lists some examples.
eg:
String Value
"157" 157
"-1.6" -1
"+50x" 50
"twelve" 0
"x506" 0
---------------------------------------------------------------------
long atol(char *ptr)
useage: return a long type.
 
---------------------------------------------------------------------
double atof(char *str)
useage: The argument str points to the string to be converted. This string can contain leading white space and a + or -- character. The number can contain the digits 0 through 9, the decimal point, and the exponent indicator E or e. If there are no convertible characters, atof() returns 0.
String Value
"12" 12.000000
"-0.123" -0.123000
"123E+3" 123000.000000
"123.1e-5" 0.001231
 
---------------------------------------------------------------------
CHARACTER TEST FUNCTIONS
The header file CTYPE.H contains the prototypes for a number of functions that test characters, returning TRUE or FALSE depending on whether the character meets a certain condition. For example, is it a letter or is it a numeral? The isxxxx() functions are actually
macros, defined in CTYPE.H.
The isxxxx() macros all have the same prototype:
    int isxxxx(int ch);
 
Macro Action
isalnum() Returns TRUE if ch is a letter or a digit.
isalpha() Returns TRUE if ch is a letter.
isascii() Returns TRUE if ch is a standard ASCII character (between 0 and 127).
iscntrl() Returns TRUE if ch is a control character.
isdigit() Returns TRUE if ch is a digit.
isgraph() Returns TRUE if ch is a printing character (other than a space).
islower() Returns TRUE if ch is a lowercase letter.
isprint() Returns TRUE if ch is a printing character (including a space).
ispunct() Returns TRUE if ch is a punctuation character.
isspace() Returns TRUE if ch is a whitespace character (space, tab, vertical tab, line feed, form feed, or carriage
return).
isupper() Returns TRUE if ch is an uppercase letter.
isxdigit() Returns TRUE if ch is a hexadecimal digit (0 through 9, a through f, A through F).
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
------------------------------ ---------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
 
---------------------------------------------------------------------
posted on 2007-10-07 03:31  sundeepblue  阅读(461)  评论(0编辑  收藏  举报