• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

几个非常有用的C字符串函数

OC/C/C++ 水墨上仙 2581次浏览

几个非常有用的C字符串函数

#include <string.h>
#include <stdlib.h>
 
void remchars(char *str, char c);
void remcnks(char *str, char *cnk);
void replchars(char *str, char c1, char c2);
void replcnks(char *str, char *cnk1, char *cnk2);
void reverse(char *str);
 
//remove specified characters from a string
void remchars(char *str, char c)
{
    char *pos;
    while(pos = strchr(str,c))
        memmove(pos, pos + 1, strlen(pos));
}
 
//remove specified chunks from a string
void remcnks(char *str, char *cnk)
{
    char *pos;
    int clen = strlen(cnk);
    while(pos = strstr(str, cnk))
        memmove(pos, pos + clen, strlen(pos) - clen + 1);
}
 
//replace specified characters in a string
void replchars(char *str, char c1, char c2)
{
    char *pos;
    while(pos = strchr(str, c1))
        *pos = c2;
}
 
//replace specified chunks in a string (size-independent, just remember about memory)
void replcnks(char *str, char *cnk1, char *cnk2)
{
    char *pos;
    int clen1 = strlen(cnk1), clen2 = strlen(cnk2);
    while(pos = strstr(str, cnk1))
    {
        memmove(pos + clen2, pos + clen1, strlen(pos) - clen1 + 1);
        memcpy(pos, cnk2, clen2);
    }
}
 
//reverse a string
void reverse(char *str)
{
    int i;
    int len = strlen(str) - 1;
    int mid = (len % 2) ? (len / 2) : ((len + 1) / 2);
    for(i = 0; i <= mid; ++i)
    {
        char buf = str[i];
        str[i] = str[len - i];
        str[len - i] = buf;
    }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明几个非常有用的C字符串函数
喜欢 (0)
加载中……