设为首页 加入收藏

TOP

C++常用字符串处理函数及使用示例
2014-11-23 19:57:18 】 浏览:219
Tags:常用 字符串 处理 函数 使用 示例

  char *strcpy(char *s1, const char *s2)
  将字符串s2复制到字符串数组s1中,返回s1的值


  char *strncpy(char *s1, const char *s2, size_t n)
  将字符串s2中最多n个字符复制到字符串数组s1中,返回s1的值


  char *strcat(char *s1, const char *s2)
  将字符串s2添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值


  char *strncat(char *s1, const char *s2, size_t n)
  将字符串s2中最多n个字符添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值


  int strcmp(const char *s1, const char *s2)
  比较字符串s1和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值


  int strncmp(const char *s1, const char *s2, size_t n)
  比较字符串s1中的n个字符和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值


  char * strtok(char *s1,const char *s2)
  用一系列strtok调用将s1字符串标记化(将字符串分成各个逻辑组件,如同一行文本中的每个单词),用字符串s2所包含的字符分隔。 首次调用时包含s1为第一个参数,后面调用时继续标记化同一字符串,包含NULL为第一个参数。每次调用时返回当前标记指针。如果函数调用时不再有更多标记,则返回NULL


  size_t strlen(const char *s)
  确定字符串长度,返回null终止符之前的字符数


  使用示例:


  //源代码在Visual c++6.0环境下编译通过
#include
#include
int main()
{
char str1[50] = "Happy birthday to ", str2[] = "coffeehu";
char temp1[100],temp2[6], * temp;
char str[] = "This is a sentence with 7 tokens";
strcpy(temp1, str1);
strncpy(temp2, str1, 5);
temp2[5] = '\0';
cout << "strcpy result: " <
cout << "strncpy result: " << temp2 << "\n";
cout << "strcat result: " << strcat(str1, str2) << "\n";
cout << "strncat result: " << strncat(str1, str2, 6) <<"\n";
cout << "strcmp result: " << strcmp(temp2,"Happ") <<"\n";
cout << "strncmp result: " << strncmp(str1,"Happy",5) <<"\n";
//strtok function eg.
temp = strtok(str, " ");
while(temp != NULL)
{
cout << temp <<'\n';
temp = strtok(NULL, " ");
}
cout << "strlen result: " << strlen(str2) <<"\n";
return 0;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇用C++Builder开发多层数据库应用.. 下一篇初学者编程入门:C++实用技巧讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目