字符串处理函数
来源:互联网 发布:筑巢软件怎么样 编辑:程序博客网 时间:2024/06/05 20:11
1. strcpy()函数
功能:拷贝一个字符串
用法:char *strcpy(char *dest, char *source);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char string[10];
char *str1 = “abcdefgh”;
strcpy(string, str1);
printf(“%s\n”,string);
return 0;
}
2. strncpy()函数
功能:串拷贝
用法:char *strncpy(char *dest, char *source, maxlen);
示例:
#include “stdio.h”
#include “string.h”
int main(void)
{
char string[10];
char *str = “abcdedf”;
strncpy(string, str, 3);
string[3]=’\0’;
printf(“%s\n”, string);
return 0;
}
3. strcat()函数
功能:字符串拼接函数
用法:char *strcat(char *dest, char *source);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char dest[25];
char *bank=” “,*c=”C++”,*Borland=”Borland”;
strcpy(dest, Borland);
strcat(dest, bank);
strcat(dest, c);
printf(“%s\n”, dest);
return 0;
}
4. strchr()、strrchr()函数
功能:strchr()在一个字符串中查找给定字符的第一个匹配之处;strrchr()函数返回一个指针,指向字符串中字符最后一次出现的位置
用法:char *strchr(char *str, char c);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char string[15];
char *ptr, c=’r’;
strcpy(string, “This is string”);
ptr = strchr(string, c);
if(ptr)
printf(“The character %c is %s\n”, c, ptr);
else
printf(“The character was not found\n”);
return 0;
}
函数返回:ring
5. strcmp()函数
功能:串比较
用法:int strcmp(char *str1, char *str2);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char *buf1 = “aaa”, *buf2 = “bbb”, *buf3 = “ccc”;
int ptr;
ptr = strcmp(buf2, buf1);
if(ptr>0)
printf(“buf2 is great then buf1\n”);
else
printf(“buf2 is less then buf1\n”);
return 0;
}
5. strcnmp()函数
功能:串比较
用法:int strncmp(char *str1, char *str2, int maxlen);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char *buf1 = “aaabbb”, *buf2 = “bbbccc”, *buf3 = “ccc”;
int ptr;
ptr = strncmp(buf2, buf1, 3);
if(ptr>0)
printf(“buf2 is greater then buf1”);
else
printf(“buf2 is less then buf1”);
return 0;
}
7. strstr()函数
功能:查找串中指定字符串首次出现的位置
用法:char *strstr(char *str1, char *str2);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char *str1 = “international”, *str2 = “nation”, *ptr;
ptr = strstr(str1, str2);
printf(“the substring is : %s\n”, ptr);
return 0;
}
返回值:national
8. strtok()函数
功能:查找由在第二个串中指定的分割符分隔开的单词
用法:char *strtok(char *str1, char *str2);
示例:
#include “stdio.h”
#include “string.h”
Int main(void)
{
char input[16] = “abc,d”;
char *p;
p = strtok(input, “,”);
printf(“%s\n”, p);
return 0;
}
返回值:abc
9. ctype.h
- PHP字符串处理函数
- 字符串处理函数
- javascript字符串处理函数
- 字符串处理函数
- c++(字符串处理函数)
- javascript字符串处理函数
- sql字符串处理函数
- JavaScript 处理字符串函数
- 两个字符串处理函数
- Javascript字符串处理函数
- JSTL字符串处理函数
- c++字符串处理函数
- [javascript]字符串处理函数
- javascript字符串处理函数
- C++字符串处理函数
- php字符串处理函数
- 字符串处理函数
- PHP字符串处理函数
- 软件项目管理
- matlab text 显示图像中或者画图中点的坐标 imshow显示图像,要用axis on显示坐标
- Android 带checkbox的listView 实现多选,全选,反选
- Struts 2的基石——拦截器(Interceptor)
- 提高web性能之--减少http请求
- 字符串处理函数
- Ubuntu C语言检测内存泄漏
- 三层结构功能解析
- 硬币问题
- 从键盘输入一个整数(1~20) 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
- java中使用配置文件做数据库连接
- Mysql JDBC Url参数说明
- PLSQL开发笔记和小结
- 关于FLASH中的LOADER问题