C语言学习(四)字符串函数

1.  strcat 字符串拼接函数 

用  法: char strcat(char 字符数组1,char 字符数组2) 
说  明: 把字符数组2拼接到字符数组1的后面
#include <string.h> 
#include <stdio.h>

int main(void) 
{ 
   char destination[25]; 
   char *blank = " ", *c = "C++", *Borland = "Borland";

   strcpy(destination, Borland); 
   strcat(destination, blank); 
   strcat(destination, c);

   printf("%s\n", destination); 
   return 0; 
} 

2. stpcpy拷贝一个字符串到另一个

用  法: char stpcpy(char 字符数组1,char 字符数组2); 
说 明:将字符串2复制到字符数组1中去
#include <stdio.h> 
#include <string.h>

int main(void) 
{ 
   char string[10]; 
   char *str1 = "abcdefghi";

   stpcpy(string, str1); 
   printf("%s\n", string); 
   return 0; 
} 

用  法: char stpcpy(字符数组1,字符数组2,int n); 
说 明:将字符串2前面n个字符复制到字符数组1中去

注意:不能用赋值语句将一个字符串常量或字符数组直接给一个字符数组 :str1=″China″; str1=str2;  

3. strcmp串比较
用  法: int strcmp(char 字符数组1,char 字符数组2);
从左到右比较Asic码

(1) 如果字符串1=字符串2,函数值为0。
(2) 如果字符串1>字符串2,函数值为一正整数。
(3) 如果字符串1<字符串2,函数值为一负整数。

4. strlen串长度

用  法:strlen(字符串)
说 明:strlen是测试字符串长度的函数。函数的值为字符串中的实际长度(不包括′\0′在内)
如:char str[10]={″China″};
    printf(″%d″,strlen(str));长度5

5. strlwr大写转小写

用  法:strlwr(字符串)
说 明:strlwr函数的作用是将字符串中大写字母换成小写字母

6. strupr小写转大写

用  法:strupr(字符串)
说 明:strupr函数的作用是将字符串中小写字母换成大写字母

posted @ 2014-12-01 16:25  W&L  阅读(266)  评论(0)    收藏  举报