c语言替换字符串 Replace the first ‘oldstr‘ with ‘newstr‘ in ‘srcstr‘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <sys/stat.h>
  
void getdate(char *datestr,char *format)
{
    time_t nnowtime = time(NULL);
    struct tm* ptmTemp = localtime(&nnowtime);
    if (ptmTemp == NULL ||
        !strftime(datestr, 256, format, ptmTemp))
        datestr[0] = '\0';
}
 
void StrToLower(char * Str)
{
    int len;
    int i;
 
    if(Str == NULL)     return;
 
    len = (int)strlen(Str);
    if(len <= 0)     return;
 
    for(i=0; i<len; i++)
    {
        if((Str[i] >= 'A') && (Str[i] <= 'Z'))
            Str[i] = 'a' + Str[i] - 'A';
    }
    return;
}
 
bool StrIsDigit(char * Str)
{
    char digitS[11]="0123456789";
    int  i = 0;
     
    for(i = 0; i < (int)strlen(Str); i++)
    {
        if(strchr(digitS,Str[i]) == NULL)
            return false;
    }
    return true;
}
 
 
/**********************************************************************************
*   Function:
*       1. Replace the first 'oldstr' with 'newstr' in 'srcstr'
*   Arguments:
*       IN  :
*           srcstr
*           oldstr
*           newstr
*       OUT :
*           No
*   Return:
*       1. If find and replace 'oldstr' with 'newstr' in 'srcstr', return 1
*       2. If find no 'oldstr' in 'srcstr', return 0
*       3. If error (malloc return NULL) return -1
***********************************************************************************/
int StrReplace(char * srcstr, const char * oldstr, const char * newstr)
{
    char    *tmpbuffer;
    int     prelen, postlen, totallen, newlen, oldlen;
    char    *ptr;
    char    *tmpchar;
 
    tmpchar = strstr(srcstr,oldstr);
    if(tmpchar == NULL) return 0;
 
    totallen=(int)strlen(srcstr);
    oldlen=(int)strlen(oldstr);
    newlen=(int)strlen(newstr);
 
    prelen = (int) (tmpchar - srcstr);
    postlen = totallen - prelen - oldlen;
    tmpchar += oldlen;
 
    tmpbuffer = (char*)malloc(prelen + newlen + postlen+1);
    if(tmpbuffer == NULL)   return -1;
 
    ptr = tmpbuffer;
    memcpy(ptr, srcstr, prelen);
    ptr += prelen;
    memcpy(ptr, newstr, newlen);
    ptr += newlen;
    memcpy(ptr, tmpchar, postlen);
 
    tmpbuffer[prelen + newlen + postlen] = 0;
    strcpy(srcstr, tmpbuffer);
    srcstr[prelen + newlen + postlen]=0;
    free((void*)tmpbuffer);
    return 1;
}
 
#include <iostream>
using namespace std;
int main()
{
  
    char szdate[64]={0};
    char sztime[64]={0};
    getdate(szdate, "%Y%m%d");
    getdate(sztime, "%H%M%S");
    std::cout<<szdate<<std::endl;
    std::cout<<sztime<<std::endl;
    char a = 'A';
    if(StrIsDigit(&a))
    std::cout<<"ewfa"<<std::endl;
    StrToLower(&a);
    std::cout<<a<<std::endl;
    char str123[100];
    strcpy(str123," my name my your name is hellowrold");
    StrReplace(str123,"my","your");
    std::cout<<str123<<std::endl;
    return 0;
}

 

posted on   lydstory  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2023-08-14 mysql表建立
2023-08-14 上海海加网络科技有限公司
2019-08-14 linux命名空间
2019-08-14 linux fcntl 对文件描述符控制
2019-08-14 Linux下Qt调用共享库文件.so
2019-08-14 zero udp
2019-08-14 protobuf使用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示