1. strncat 函数:

  【函数原型】#include <string.h>

        char *strncat( char *str1, const char *str2, size_t count );

  【功能】将字符串str2 中至多count个字符连接到字符串str1中,追加空值结束符。返回处理完成的字符串。

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h> void main()
{
char str1[] = "hello world";
char str2[] = "1234.567"; strncat(str1, str2, ); //从str2中拷贝4个字节到str1中 printf("%s\n", str1); //hello world1234
system("pause");
return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h> void mystrncat(char *str1, const char *str2, int count)
{
if (str1 == NULL || str2 == NULL)
return; char *p = str1;
while (*p != '\0')
{
p++;
} //p指向了str1的末端'\0'
for (int i = ; i < count; i++) //前进count个字符
{
*p = str2[i]; //赋值
p++; //指针前进
}
*p = '\0';
} void main()
{
char str1[] = "hello world";
char str2[] = "1234.567"; //strncat(str1, str2, 4); //从str2中拷贝4个字节到str1中
mystrncat(str1, str2, ); //从str2中拷贝4个字节到str1中 printf("%s\n", str1); //hello world1234
system("pause");
return ;
}

2. atoi 函数:

  【函数原型】#include <stdlib.h>

        int atoi( const char *str );

  【功能】将字符串str转换成一个整数并返回结果。参数str 以数字开头,当函数从str 中读到非数字字符则结束转换并将结果返回。例如,

      i = atoi( "512.035" );

      i 的值为 512

  【库函数使用】

#include <stdio.h>
#include <stdlib.h> void main()
{
//char str[10] = "8848";
//int num = atoi(str);
//printf("%d\n", num); //8848 //char str[10] = "8848";
//int num = atoi(str+1);
//printf("%d\n", num); //848 //转换的时候,传递字符串的首地址,但地址不要求是首地址
//字符串的任何地址都可以,num起到接收赋值的作用
//转换成功是整数,失败是0,出现非数字字符都会转换失败
char str[] = "e8848";
int num = atoi(str);
printf("%d\n", num); // system("pause");
return ;
}

  【库函数实现】

3. strrev 函数:

  【函数原型】#include <string.h>

        void strrev( char *str);

  【功能】将字符串str逆转。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str[] = "hello8848";
printf("原来字符串:%s\n", str); //hello8848 _strrev(str); //_strrev与strrev用法完全一样 printf("后来字符串:%s\n", str); //8488olleh system("pause");
return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void mystrrev(char *str)
{
int len = strlen(str); //获取字符串长度
for (int i = ; i < (len / ); i++) //循环一半,交换字符
{
char ch = str[i]; //对调字符
str[i] = str[len - - i]; //数组a[i]最大的元素是a[i-1]
str[len - - i] = ch;
}
} void main()
{
char str[] = "hello8848";
printf("原来字符串:%s\n", str); //hello8848 //_strrev(str); //_strrev与strrev用法完全一样
mystrrev(str); printf("后来字符串:%s\n", str); //8488olleh system("pause");
return ;
}

4. strupr 函数 与 strlwr 函数:

  【函数原型】void strupr(char *str);

        void strlwr(char *str);

  【功能】strupr()是将字符串小写转为大写;strlwr()是将字符串大写转为小写。

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str[] = "notepad"; _strupr(str); //小写转大写
printf("%s\n", str); //NOTEPAD _strlwr(str); //大写转小写
printf("%s\n", str); //notepad system("pause");
return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void toBig(char *str)
{
while (*str != '\0') //判断字符串是否结束
{
if ((*str) >= 'a' && (*str) <= 'z') //判断是否小写字母
{
*str = *str - ; //小写转大写 A--65 a--97 }
str++; //继续循环
}
} void toSmall(char *str)
{
while (*str != '\0') //判断字符串是否结束
{
if ((*str) >= 'A' && (*str) <= 'Z') //判断是否大写字母
{
*str = *str + ; //大写转小写 A--65 a--97 }
str++; //继续循环
}
} void main()
{
char str[] = "notepad"; //_strupr(str); //小写转大写
toBig(str); //小写转大写
printf("%s\n", str); //NOTEPAD //_strlwr(str); //大写转小写
toSmall(str); //大写转小写
printf("%s\n", str); //notepad system("pause");
return ;
}

5. strlen 函数:

  【函数原型】#include <string.h>

        size_t strlen( char *str );

  【功能】函数返回字符串str 的长度( 即空值结束符之前字符数目)。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str[] = "I love iphone"; //str是变量
char *p = "I love china"; //p是常量 int len1 = strlen(str);
int len2 = strlen(p); printf("str=%d,p=%d\n", len1, len2); //str=13,p=12 system("pause");
return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> int mystrlen(char *str)
{
int len = ; //长度
while (*str != '\0') //检测字符串是否结束
{
len++; //继续计数
str++; //指针继续前进
}
return len;
} void main()
{
char str[] = "I love iphone"; //str是变量
char *p = "I love china"; //p是常量 //int len1 = strlen(str);
//int len2 = strlen(p); int len1 = mystrlen(str);
int len2 = mystrlen(p); printf("str=%d,p=%d\n", len1, len2); //str=13,p=12 system("pause");
return ;
}

6. strcpy 函数 与 strcat 函数:实现两个字符串连接,放到第三个字符串中

  【函数原型】#include <string.h>

        char *strcpy( char *to, const char *from );

        char *strcat( char *str1, const char *str2 );

  【功能】strcpy:复制字符串from 中的字符到字符串to,包括空值结束符。返回值为指针to

      strcat :函数将字符串str2 连接到str1的末端,并返回指针str1.

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str1[] = "note";
char str2[] = "pad";
char str[]; //方法一:使用sprintf字符串打印函数
//sprintf(str, "%s%s", "note", "pad");
//printf("%s\n", str); //notepad //方法二:库函数strcpy和strcat
strcpy(str, str1); //字符串拷贝
printf("%s\n", str); //note
strcat(str, str2); //字符串连接
printf("%s\n", str); //notepad system("pause");
return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void * mystrcpy(char *to, char *from)
{
char *p = to; //备份首地址
if (to == NULL || from == NULL)
return NULL; //字符串为空,不需要拷贝 while (*from != '\0')
{
*to = *from; //一个字符一个字符的拷贝
to++;
from++;
}
*to = '\0';
return p;
} void * mystrcat(char *strDest, char *strSrc)
{
char *p = strDest; //备份首地址
if (strDest == NULL || strSrc == NULL)
return NULL; while (*strDest != '\0') //循环到strDest末尾'\0'
{
strDest++;
}
while (*strSrc != '\0')
{
*strDest = *strSrc;
strDest++;
strSrc++;
}
*strDest = '\0';
return p;
} void main()
{
char str1[] = "note";
char str2[] = "pad";
char str[]; //方法一:使用sprintf字符串打印函数
//sprintf(str, "%s%s", "note", "pad");
//printf("%s\n", str); //notepad //方法二:库函数strcpy和strcat
//strcpy(str, str1); //字符串拷贝
mystrcpy(str, str1); //字符串拷贝
printf("%s\n", str); //note
//strcat(str, str2); //字符串连接
mystrcat(str, str2); //字符串连接
printf("%s\n", str); //notepad system("pause");
return ;
}

7. strchr 函数:

  【函数原型】#include <string.h>

        char *strchr( const char *str, int ch );

  【功能】函数返回一个指向strch 首次出现的位置,当没有在str 中找ch到返回NULL。

  【库函数使用】

#include <stdio.h>
#include <string.h> void main()
{
char str[] = "I love iphone";
char ch = 'v'; char *p = strchr(str, ch); if (p == NULL)
printf("没有找到\n");
else
printf("值%c,地址%x.\n", *p, p); system("pause");
return ;
}

  【库函数实现】

#include <stdio.h>
#include <string.h> char *mystrchr(char *str, char ch)
{
if (str == NULL)
return NULL; else
{
while (*str != '\0')
{
if (*str == ch) //相等,跳出循环
return str;
str++; //指针向前移动
}
} return NULL;
} void main()
{
//char str[20] = "I love iphone";
//char ch = 'v';
//char *p = strchr(str, ch); char str[] = "I love iphone";
char ch = 'a';
char *p = mystrchr(str, ch); if (p == NULL)
printf("没有找到\n");
else
printf("值%c,地址%x.\n", *p, p); system("pause");
return ;
}

8. strcmp 函数:

  【函数原型】#include <string.h>

        int strcmp( const char *str1, const char *str2 );

  【功能】比较字符串str1 and str2, 返回值为0表示相等,返回不为0表示不等。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str1[] = "note";
char str2[] = "note"; if (strcmp(str1, str2) == )
printf("相等\n"); //相等
else
printf("不等\n"); system("pause");
return ;
}

  【库函数实现】 

#include <stdio.h>
#include <stdlib.h>
#include <string.h> int mystrcmp(char *str1, char *str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2); if (len1 != len2)
return -;
else
{
int flag = ; //假定相等
for (int i = ; i < len1; i++)
{
if (str1[i] != str2[i]) //出现一个字符不等
{
flag = ;
break;
}
if (flag == )
return ;
else
return -;
}
}
} void main()
{
char str1[] = "note";
char str2[] = "note"; //if (strcmp(str1, str2) == 0)
// printf("相等\n"); //相等
//else
// printf("不等\n"); if (mystrcmp(str1, str2) == )
printf("相等\n"); //相等
else
printf("不等\n"); system("pause");
return ;
}

9. strstr 函数:

  【函数原型】#include <string.h>

          char *strstr( const char *str1, const char *str2 );

  【功能】函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL。

  【库函数使用】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str1[] = "I love iphone I love app";
char str2[] = "iphone"; char *p = strstr(str1, str2); //检索子串 if (p == NULL)
printf("没有找到\n");
else
printf("值%c,地址%x\n", *p, p); //值i,地址... system("pause");
return ;
}

  【库函数实现】

#include <stdio.h>
#include <stdlib.h>
#include <string.h> char *mystrstr(char *str1, char *str2)
{
if (str1 == NULL || str2 == NULL)
return NULL; int len1 = strlen(str1); //母串的长度
int len2 = strlen(str2); //子串的长度 for (int i = ; i < len1 - len2; i++)
{
int flag = ; //标记,假定字符串一开始相等 for (int j = ; j < len2; j++)
{
if (str1[i + j] != str2[j]) //有一个字符不等
{
flag = ;
break; //有一个不等跳出循环
}
}
if (flag == )
{
return (str1 + i); //返回找到的地址
break;
}
}
return NULL;
} void main()
{
char str1[] = "I love iphone I love app";
char str2[] = "iphone"; //char *p = strstr(str1, str2); //检索子串
char *p = mystrstr(str1, str2); //检索子串 if (p == NULL)
printf("没有找到\n");
else
printf("值%c,地址%x\n", *p, p); //值i,地址... system("pause");
return ;
}

 

10. strset 函数:

  【函数原型】#include <string.h>

          char *strset(char *str, char ch );

  【功能】将字符串设置为制定字符,常用于字符串清0操作。

  【库函数使用】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void main()
{
char str[] = "helloworld8832";
printf("原来的str:%s\n", str); //helloworld8832 _strset(str, ''); printf("修改后的str:%s\n", str); // system("pause");
return ;
}

  【库函数实现】

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void mystrset(char *str, char ch)
{
while (*str != '\0') //遍历字符串
{
*str = ch; //赋值字符
str++; //字符串指针不断向前
}
} void main()
{
char str[] = "helloworld8832";
printf("原来的str:%s\n", str); //helloworld8832 //_strset(str, '8');
mystrset(str, ''); printf("修改后的str:%s\n", str); // system("pause");
return ;
}

5. 常见C语言字符串库函数的使用及实现的更多相关文章

  1. c语言字符串库函数#include<string.h>

    字符串函数<string.h> 在头文件<string.h>中定义了两组字符串函数.第一组函数的名字以str开头:第二组函数的名字以mem开头.只有函数memmove对重叠对象 ...

  2. C语言字符串库函数的实现

    1.strlen(字符串的长度) size_t Strlen(const char* str) { assert(str); ;; ++i) { if (str[i] == '\0') return ...

  3. C语言字符串操作常用库函数

    C语言字符串操作常用库函数 *********************************************************************************** 函数 ...

  4. C语言讲义——字符串库函数

    字符串库函数<string.h> 求字符串长度(不含结束符'\0'****) strlen(str) 字符串赋值(可能造成数组越界) strcpy(str," 水浒传 " ...

  5. 转:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文

    转自:C语言字符串操作函数 - strcpy.strcmp.strcat.反转.回文 C++常用库函数atoi,itoa,strcpy,strcmp的实现 作者:jcsu C语言字符串操作函数 1. ...

  6. C 语言字符串连接的 3种方式

    C 语言字符串连接的 3种方式 #include<stdio.h> #include<stdlib.h> #include<string.h> char *join ...

  7. C.【转】C语言字符串与数字相互转换

    1.gcvt 把浮点数转成字符串 - CSDN博客.html(https://blog.csdn.net/dxuehui/article/details/52791412) 1.1. 函数名: gcv ...

  8. C语言中库函数strstr的实现

    在C语言中库函数strstr()函数表示在一个字符串str1中查找另一个字符串str2,如果查到则返回str2在str1中首次出现的位置,如果找不到则返回null. char* strstr(char ...

  9. C语言字符串的操作

    C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. 字符串求长 - strlen5. 字符串连接 - strcat6. ...

随机推荐

  1. Oracle 10g RAC 如何配置 VIP IPMP

    metalink note 283107.1介绍了如何设置VIP的IPMP,此处记录一下设置过程. o Existing 10g RAC installation ^^^^^^^^^^^^^^^^^^ ...

  2. cuteFTP连接不上VM虚拟机中RedHat&amp;…

    摸索了一下午,终于解决了问题:主要原因是因为redhat系统配置文件默认root用户无法使用ftp,只需作如下修改就可以使用了.            1.找到/etc/vsftpd/目录修改下面的连 ...

  3. Zookeeper Api(java)入门与应用

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  4. thinkphp对mysql的CURD操作

    利用thinkphp(3.2.3)来操作数据库,首先要连接数据库.我们需要对某数据库写一个配置文件,thinkphp会根据该配置文件自动连接上数据库.而model文件就不用自定义,内置的即可解决问题. ...

  5. laravel 队列

    php artisan  queue:table 先创建job 队列表 php artisan migrate 执行表 php artisan make:job SendMessage 创建一个job ...

  6. algorithm notes

    1.算法可视化 https://visualgo.net/en

  7. Python pandas.DataFrame调整列顺序及修改index名

    1. 从字典创建DataFrame >>> import pandas >>> dict_a = {'],'mark_date':['2017-03-07','20 ...

  8. Socket接口原理及用C#语言实现

    首先从原理上解释一下采用Socket接口的网络通讯,这里以最常用的C/S模式作为范例,首先,服务端有一个进程(或多个进程)在指定的端口等待客户来连接,服务程序等待客户的连接信息,一旦连接上之后,就可以 ...

  9. 跨库连接报错Server 'myLinkedServer' is not configured for RPC

    Solution: Problem is most likely that RPC is not configured for your linked server. That is not a de ...

  10. razor自定义函数 @helper 和@functions小结

    from:http://www.cnblogs.com/jiagoushi/p/3904995.html asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的 ...