几个一定要掌握的string 函数

 

转载自http://www.cnblogs.com/sundeepblue/archive/2007/10/07/915809.html

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

函数调用strcat(s1,s2)是将s2字符串复制连接到s1字符串之后,使s1字符串变得更长。函数调用strcpy(s1,s2)是将s2字符串复制到s1,使s1字符串的内容与s2字符串的内容相同。

 

strcat() 将字串接合到目标字串的末尾 
strcmp() 比较两个字串是否相等 
strcmpi() 比较两个字串是否相等,不考虑大小写 
strcpy() 将字串内容复制到目标字串中 
strstr() 扫描字串中第一个出现的字串 
strlen() 返回字串长度 
strupr() 将字串中的所有字符变成大写 
sprintf() 根据几个参数建立字串 
一下是英文的介绍:
/* 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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值