c: string

 

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
/**
 * *****************************************************************************
 * @file        duSortType.h
 * @brief      
 * @author       ()
 * @date        2023-10-18
 * @copyright   geovindu
 * *****************************************************************************
 */
 
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
 
/**
 * @brief   整数从大到小排序   
 * @param a
 * @param b
 * @return
 */
int IntCmpDesc(const void *a,const void *b);
 
/**
 * @brief     整数排序从小到大 
 * @param a
 * @param b
 * @return
 */
int IntCmpAsc(const void *a,const void *b);
 
 
 
/**
 * @brief     排序从小到大
 * @param a
 * @param b
 * @return
 */
int strCmpAsc(const void *srca,const void *srcb);
 
/**
 * @brief     排序从大到小
 * @param a
 * @param b
 * @return
 */
int strCmpDsc(const void *srca,const void *srcb);

  

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
/**
 * *****************************************************************************
 * @file        duSortType.c
 * @brief      
 * @author       ()
 * @date        2023-10-18
 * @copyright   geovindu
 * *****************************************************************************
 */
 
#include"include/duSortType.h"
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
 
 
/**
 * @brief   整数从大到小排序   
 * @param a
 * @param b
 * @return
 */
int IntCmpDesc(const void *a,const void *b)
{
    return *(int*)b-*(int*)a;
}
/**
 * @brief     整数排序从小到大 
 * @param a
 * @param b
 * @return
 */
int IntCmpAsc(const void *a,const void *b)
{
    return *(int*)a-*(int*)b;
}
 
 
/**
 * @brief     排序从小到大
 * @param a
 * @param b
 * @return
 */
int strCmpAsc(const void *a,const void *b)
{
    return *(char*)a-*(char*)b;
}
 
/**
 * @brief     排序从大到小
 * @param a
 * @param b
 * @return
 */
int strCmpDsc(const void *a,const void *b)
{
    return *(char*)b-*(char*)a;
}

  

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*****************************************************************//**
 * \file   Dustring.h
 * \brief
 * IDE: VSCODE   c11
 *
 * int isdigit(int c)   如果c是一个数字,返回true,否则返同 false
 * int isalpha(int c)  如果c是一个字母,返回true,否则返回 false
 * int isalnum(int c)  如果 c 是一个字母或数字,返回 true,否则返同 false
 * int isxdigit(int c)  如果c是一个十六进制字符,返回 true,否则返回 false
 * int islower(int c)  如果c是一个小写字母,返回 true,否则返回 false
 * int isupper(int c)  如果c是一个大写字母,返回true,否则返回 false
 * int isspace(int c)   如果c是一个空白符,返回 true,否则返回 false 空白符包括,"n,空格,W,V进纸符(),垂直制表符(“v)
 * int iscntrl(int c)  如果c是一个控制符,返回 true ,否则返回 false
 * int ispunct(int c)   如果c是一个除空格、数字和字母外的可打印字符,返回 true,否则返回 false
 * int isprint(int c)  如果c 是一个可打印符 (包括空格),返回 true,否则返回 false
 * int isgraph(int c)   如果c 是除空格之外的可打印字符,返回 true,否则返回 false
  
 * 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * \author geovindu,Geovin Du 涂聚文
 * \date   2023-10-15
***********************************************************************/
  
#ifndef DUSTRING_H
#define DUSTRING_H
  
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
 
  
/**
 * @brief 从左截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strLeft(char *dst,char *src,int n);
  
  
/**
 * @brief 从右截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strRight(char *dst,char *src,int n);
  
  
/**
 * @brief 截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param start 开始位置
 * @param len 取几位
 * @return char*  返回要截取的字符串
 */
char *strSubstring(char *dst,char *src,int start,int len);
  
/**
 * @brief 字符串替换
 *
 * @param search
 * @param replace
 * @param subject
 * @param bufsize
 * @return char*
 */
char *strReplace(const char *search, const char *replace, char *subject, int bufsize);
  
/**
 * @brief  删除两端空格
 *
 * @param scr
 * @return int
 */
int trim(char *scr);
  
  
  
/**
 * @brief 字符字替换
 *
 * @param src 原字符串
 * @param oldstr  要替的旧字串
 * @param newstr  要替成的新字串
 * @return char* 返回替好的字符串
 */
char *duReplace(char *src,char *oldstr,char *newstr);
  
  
/**
 * @brief 排序OK     
 * @param 原字符串数组
 * @param 排序后的字符串数组
 * @returns 返回值 不OK null
 */
char* duSortAsc(char* src,char* newchar,int lenght);
 
 
/**
 * @brief 排序OK
 * @param 原字符串数组
 * @param  排序后的字符串数组
 * @returns 返回值 OK
 */
char* duSortAsc2(char* src,char* newchar[],int lenght);
 
 /**
 * @brief      
 * @param  dst
 * @param str
 * @param spl
 * 示例 
 *   char str[] = "what is your name?";
 *   char dst[10][80];
 *   int cnt = split(dst, str, " ");
 *   for (int i = 0; i < cnt; i++)
 *       puts(dst[i]);
 *
 */
int strSplit(char dst[][80], char* str, const char* spl);
 
 
/**
 * @brief   排序从大到小    
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
char* duStrCmpSortAsc(char* src,char* newchar[],int lenght);
 
/**
 * @brief   排序从大到小    
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
char* duStrCmpSortDesc(char* src,char* newchar[],int lenght);
 
 
/**
 * @brief   排序从小到大
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
int* duIntCmpSortAsc(int* src,int* newint[],int lenght);
 
 
 
/**
 * @brief   排序从大到小   
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
int* duIntCmpSortDesc(int* src,int* newint[],int lenght);
 
 
 
 
#endif

  

 

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*****************************************************************//**
 * \file   Dustring.c
 * \brief  字符串操作
 * IDE: VSCODE   c11
 *站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 * \author geovindu,Geovin Du 涂聚文
 * \date   2023-10-15
***********************************************************************/
  
#include "include/Dustring.h"
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include "include/duSortType.h"
 
/**
 * @brief 从左截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strLeft(char *dst,char *src,int n)
{
      
    char *p=src;
    char *q=dst;
    int len=strlen(src);
    //isupper()
    //islower()
  
    if(n>len) n=len;
    while(n--) *(q++)=*(p++);
    *(q++)='\0';
    return dst;
}
  
/**
 * @brief 从右截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strRight(char *dst,char *src,int n)
{
    char *p=src;
    char *q=dst;
    int len=strlen(src);
    if(n>len) n=len;
    p+=(len-n);
    while(*(q++)=*(p++));
    return dst;
}
  
/**
 * @brief 截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param start 开始位置
 * @param len 取几位
 * @return char*  返回要截取的字符串
 */
char *strSubstring(char *dst,char *src,int start,int len)
{
    char *p=dst;
    char *q=src;
    int length=strlen(src);
    if(start>=length||start<0)
        return NULL;
    if(len>length)
        len=length-start;
    q+=start;
    while(len--)
    
        *(p++)=*(q++);
    }
    *(p++)='\0';
    return dst;
}
  
  
/**
 * @brief 字符串替换
 *
 * @param search
 * @param replace
 * @param subject
 * @param bufsize
 * @return char*
 */
char *strReplace(const char *search, const char *replace, char *subject, int bufsize)
{
    char *p;
    int i, j = 0, n = 0;
    int len, slen, rlen, tlen;
      
    // check arguments
    if (subject == NULL)
    {
        return NULL;
    }
    tlen = strlen(subject);
    if (bufsize == -1)
    {
        // make sure subject is writable
        p = malloc(tlen + 1);
        if (p == NULL)
        {
            return NULL;
        }
        memcpy(p, subject, tlen + 1);
        subject = p;
    }
    if (search == NULL || search[0] == '\0')
    {
        return subject;
    }
    slen = strlen(search);
    if (replace == NULL)
    {
        replace = "";
    }
    rlen = strlen(replace);
   
    // find out how many replacements are needed
    for (i = 0; subject[i] != '\0'; i++)
    {
        if (subject[i] != search[j])
        {
            // character mismatch
            j = 0;
        }
        if (subject[i] == search[j])
        {
            j++;
            if (j == slen)
            {
                // pattern match
                subject[i + 1 - j] = '\0'; // front
                subject[i] = '\0'; // rear
                j = 0;
                n++;
            }
        }
    }
    if (n == 0)
    {
        return subject;
    }
   
    // calculate the length of the new string and check buffer size
    len = tlen + n * (rlen - slen);
    if (bufsize == -1)
    {
        if (len > tlen)
        {
            // increase buffer size
            p = realloc(subject, len + 1);
            if (p == NULL)
            {
                free(subject);
                return NULL;
            }
            subject = p;
        }
    }
    else if (bufsize < len + 1)
    {
        return NULL;
    }
   
    // replace substrings
    if (len <= tlen)
    {
        i = 0; // for read
        j = 0; // for write
        while (i < tlen)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j, replace, rlen);
                i += slen;
                j += rlen;
            }
            else
            {
                subject[j] = subject[i];
                i++;
                j++;
            }
        }
    }
    else
    {
        i = tlen - 1;
        j = len - 1;
        while (i >= 0)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j + 1 - rlen, replace, rlen);
                i -= slen;
                j -= rlen;
            }
            else
            {
                subject[j] = subject[i];
                i--;
                j--;
            }
        }
    }
    subject[len] = '\0';
    return subject;
}
   
/**
 * @brief  删除两端空格
 *
 * @param scr
 * @return int
 */
int trim(char *scr)
{
    int i, j = -1, len = 0;
   
    for (i = 0; scr[i] != '\0'; i++)
    {
        if (j == -1 && scr[i] != ' ' && scr[i] != '\t')
        {
            j = 0;
        }
        if (j != -1)
        {
            scr[j] = scr[i];
            j++;
            if (scr[i] != ' ' && scr[i] != '\t')
            {
                len = j;
            }
        }
    }
    scr[len] = '\0';
    return len;
}
  
/**
 * @brief 字符字替换
 *
 * @param src 原字符串
 * @param oldstr  要替的旧字串
 * @param newstr  要替成的新字串
 * @return char* 返回替好的字符串
 */
char *duReplace(char *src,char *oldstr,char *newstr)
{
  
    char bstr[strlen(src)];//转换缓冲区
    memset(bstr,0,sizeof(bstr));
    for(int i = 0;i < strlen(src);i++){
            if(!strncmp(src+i,oldstr,strlen(oldstr))){//查找目标字符串
                strcat(bstr,newstr);
                i += strlen(oldstr) - 1;
            }
            else
            {
                strncat(bstr,src + i,1);//保存一字节进缓冲区
            }
    }
    strcpy(src,bstr);
    return src;
  
}
 
/**
 * @brief 排序OK      从大到小
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @returns 返回值 不OK null
 */
char* duSortDesc(char* src,char* newchar,int lenght)
{
    char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    //printf("%d\n",lentf);
    //char* newstr[lenght];
    int digits[10] = {0}; // 存储数字出现的次数
    for (int i = 0; src[i] != '\0'; i++) {
        if (isdigit(src[i])) {
            digits[src[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digits[i]; j++) {
            char digitChar = i + '0';
             
            //*(newchar+j)=digitChar;
            //*(newchar+i)=digitChar;
            //printf("%c",digitChar);
            //newchar[j]=digitChar;
            strncat(newchar, &digitChar, 1);
            //strncat(newstr, &digitChar, 1);
        }
    }
 
    return newchar;  //newstr 返回这个值有点不准确
 
}
 
/**
 * @brief 排序OK  从大到小
 * @param  src 原字符串数组
 * @param  newchar 排序后的字符串数组
 * @param lenght 字符串数组长度
 * @returns 返回值 OK
 */
char* duSortDesc2(char* src,char* newchar[],int lenght)
{
    char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    //const lstr=lenght;
    //printf("%d\n",lentf);
    //char* newstr[lentf];
    int digits[10] = {0}; // 存储数字出现的次数
    for (int i = 0; src[i] != '\0'; i++) {
        if (isdigit(src[i])) {
            digits[src[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digits[i]; j++) {
            char digitChar = i + '0';
             
            //*(newchar+j)=digitChar;
            //*(newchar+i)=digitChar;
            //printf("%c",digitChar);
            //newchar[j]=digitChar;
            strncat(newchar, &digitChar, 1);
            //strncat(newstr, &digitChar, 1);
        }
    }
 
    return newchar;
 
}
 
/**
 * @brief      
 * @param  dst
 * @param str
 * @param spl
 * 示例 
 *   char str[] = "what is your name?";
 *   char dst[10][80];
 *   int cnt = split(dst, str, " ");
 *   for (int i = 0; i < cnt; i++)
 *       puts(dst[i]);
 *
 */
int strSplit(char dst[][80], char* str, const char* spl)
{
    int n = 0;
    char *result = NULL;
    result = strtok(str, spl);
    while( result != NULL )
    {
        strcpy(dst[n++], result);
        result = strtok(NULL, spl);
    }
    return n;
}
 
 
 
/**
 * @brief   排序从小到大
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
int* duIntCmpSortAsc(int* src,int* newint[],int lenght)
{
    int bstr[sizeof(src)/sizeof(src[0])];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    int* newstr[lentf];
    qsort(src,strlen(src),sizeof(src[0]),IntCmpAsc);
    for(int i=0;i<lenght;i++)
    {
        printf("%d ",src[i]);
        strncat(newint,src[i], 1);
        strncat(newstr, src[i], 1);
    }      
    return newint;
}
 
/**
 * @brief   排序从大到小   
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
int* duIntCmpSortDesc(int* src,int* newint[],int lenght)
{
    int bstr[sizeof(src)/sizeof(src[0])];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    int* newstr[lentf];
    qsort(src,strlen(src),sizeof(src[0]),IntCmpDesc);
    for(int i=0;i<lenght;i++)
    {
        printf("%d ",src[i]);
        strncat(newint,src[i], 1);
        strncat(newstr, src[i], 1);
    }      
    return newint;
}
 
 
 
 
/**
 * @brief   排序从大到小    
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
char* duStrCmpSortAsc(char* src,char* newchar[],int lenght)
{
    char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    char* newstr[lentf];
    qsort(src,strlen(src),sizeof(src[0]),strCmpAsc);
    for(int i=0;i<lenght;i++)
    {
        printf("%c ",src[i]);
        strncat(newchar,src[i], 1);
        strncat(newstr, src[i], 1);
    }      
    return newchar;
}
 
/**
 * @brief   排序从大到小    
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度 
 * @return
 *
 */
char* duStrCmpSortDesc(char* src,char* newchar[],int lenght)
{
    //char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    //memset(bstr,0,sizeof(bstr));
    const int lentf=strlen(src);
    printf("原始字符串:%s \n",src);
    //int firstn=sizeof(src[0]);
    char* newstr[lenght];
    qsort(src,strlen(src),sizeof(src[0]),strCmpDsc);
 
    for(int i=0;i<strlen(src);i++)
    {
        //printf("%c ",src[i]);
        //strncat(newchar,src[i], 1);
        if (isdigit(src[i])){
            char va=src[i] ;
            printf("%c ",va);
            strncat(newstr, &va, 1);
            strncat(newchar, &va,1);
        }
        else
        {
            char va=src[i];
            strncat(newstr, &va, 1);
            strncat(newchar, &va,1);
 
        }
        //newchar[i]=src[i];       
        //newstr[i]=src[i];
    }      
    return newchar;
}

 

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
printf("hello c world \n");
printf("你好,中国\n");
//char a[10]="1092367145"; //变量名重复赋值,这个编译出问题
 
char bbdu[10]="1092367145";
 
/*
char* c[10]={'\0'};
qsort(a,strlen(a),sizeof(a[0]),cmp);
for(int i=0;i<=10;i++)
{
    char du=a[i];
    printf("%c ",du);
    strncat(c, &du, 1);
    //c[i]=du;
}
printf("\nc=:%s length=:%d\n",c,strlen(c));
*/
char* charqs[10]={'\0'};//NULL; //
char* reqs;   
 
 
printf("\n 原串: %s",bbdu);
reqs=duStrCmpSortDesc(bbdu,charqs,10);  
printf("\nchar:%s lenght=:%d\n",charqs,strlen(charqs));
printf("\nchar:%s\n",reqs);
 
 
 
//分割字符串   https://githubmota.github.io/2017/12/29/2017-12-29-Linux-C-Split/
//如何找到数字字符串的首位数和末位数索引
char ssss[] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"
char delim[] = " ,!"
 
char *token; 
for(token = strtok(ssss, delim); token != NULL; token = strtok(NULL, delim)) { 
    printf(token); 
    printf("\n\f"); 
printf("\n");
  
 
 
 
 
char *dustr = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //这个不可以用替换函数,需要索引长度大于自身的宽度
char *substr = "109236714533";
char *substr2 = "654123";
char geovindu[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //必须索引值大,否则不可以替换
char *newdig[10];
char *digg;
char *sdu;
char *newdig2[6];
 
 
 
 
char newstrd[10]={0};//初始化赋值
char *ddu[10]={'\0'};; //初始化赋值
int digitsdu[10] = {0}; // 存储数字出现的次数
for (int i = 0; substr[i] != '\0'; i++) {
    if (isdigit(substr[i])) {
        digitsdu[substr[i] - '0']++;
    }
}
for (int i = 9; i >= 0; i--) {
    for (int j = 0; j < digitsdu[i]; j++) {
        char digitChar = i + '0';
        strncat(newstrd, &digitChar, 1);
        //printf("%c",digitChar);
        strncat(ddu, &digitChar, 1);
    }
}
 
char newstrd2[6]={0}; //初始化赋值
char *ddu2[6]={'\0'};//初始化赋值
int digitsdu2[10] = {0}; // 存储数字出现的次数
for (int i = 0; substr2[i] != '\0'; i++) {
    if (isdigit(substr2[i])) {
        digitsdu2[substr2[i] - '0']++;
    }
}
for (int i = 9; i >= 0; i--) {
    for (int j = 0; j < digitsdu2[i]; j++) {
        char digitChar = i + '0';
        strncat(newstrd2, &digitChar, 1);
        //*(newchar+j)=digitChar;
        //*(newchar+i)=digitChar;
        //printf("%c",digitChar);
        //ddu2[i]=digitChar;
        strncat(ddu2, &digitChar, 1);
    }
}
 
printf("\n1: %s\n",newstrd);
printf("2: %s\n",ddu);
printf("3: %s\n",newstrd2);
printf("4: %s\n",ddu2);
 
 
 
 
char *newddd=newstrd;
char *fff;
char *kk;
 
char dustr11[10] = "1092367145";
char dustr22[6] = "654123";
digg=duStrCmpSortDesc(dustr11,newdig,10);
kk=duStrCmpSortDesc(dustr22,newdig2,6);
char* Olddustr11 = "1092367145";
char* Olddustr22 = "654123";
fff=duReplace(geovindu,Olddustr11,ddu); //ddu
printf("fff1:%s\n",fff);
fff=duReplace(geovindu,Olddustr22,ddu2); 
printf("fff2:%s\n",fff);
printf("\nnew:%s\n",digg);
printf("newdig:%s\n",newdig);
printf("newdig2:%s\n",newdig2);
printf("kk:%s\n",kk);
printf("fff3:%s\n",fff);
 
 
 
//查找索引
char *dus =strstr(dustr,substr); //
char *dus2=strstr(dustr,substr2);//
if(dus==NULL)
    printf("can't find %s in %s\n",substr,dustr);
else
    printf("%s include %s;show the string from start found address:%s\n", dustr,substr,dus);
 //起始索引  
 int index=dus - dustr;
 int index2=dus2-dustr;
 //结束索引
 int endindex=index+strlen(substr);
 int endindex2=index2+strlen(substr2);
 printf("1092367145 start index:%d,end:%d.\n",index,endindex);
 printf("654123 start index:%d,end:%d.\n",index2,endindex2);  
 
char ch[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi";
char duresult[100]; // 存储处理后的结果
 
char gx[]="GXZX";
 
char *dup;
char *gxdup;
char newstr[100];
char *des;
dup=fff; //fff
char gxz[]="高训中心";
//strcat(newstr, "高训中心");   
strncat(newstr,&gxz,8); 
//printf("%s/n",gxdup);
int gxlen=strlen("高训中心");
//*(dup+14)='d';
int l=sizeof(ch)/sizeof(ch[0]);
//倒序显示
//for(int i=l;i>=0;i--)
//{
    // printf("%c\n",dup[i]); //
     
// }
//顺序显示
for(int i=4;i<l;i++)
{
 
    if(dup[i]>='A' && dup[i]<='Z')
    {
        printf("%c\n",dup[i]);
        char sd=dup[i];
        strncat(newstr,&sd,1); 
 
    }
    else if(dup[i]>='a' && dup[i]<='z')
    {
        printf("%c\n",toupper(dup[i]));
        *(dup+i)=toupper(dup[i]);
        char sx=toupper(dup[i]);
         strncat(newstr,&sx,1);              
    }
    else if(isdigit(dup[i]))
    {
        printf("數字:%c\n",dup[i]);
        char sszi=dup[i];
        strncat(newstr,&sszi,1);  
    }
    else
    {
 
        //des=newstr;
        //des=dup[i];
        //strcat(des[1],dup[i]);  //strcpy(des, dup[i]);
        //memset(des, '\0', sizeof(des));
        //strcat(&des,&dup[i]);
        //*(des+i) = '*';
        //*(des+i+1)=dup[i];
        //printf("%c\n",dup[i]);
        strncat(newstr, &ch[i], 1);
        strncat(newstr, &ch[i], 1);
    }
    //
     
}
gxdup=duReplace(ch,"GXZX","高训中心");
printf("gx=%s\n",gxdup);
printf("newstr=  %s\n",newstr);
// (1) 将GXZX前四个字符串中的大写字母转换成“高训中心”
for (int i = 0; i < 4; i++) {
    if (isupper(ch[i])) {
        strcat(duresult, "高训中心");
    } else {
        // (2) 将字符串中其余的小写字母转换成大写字母
        char uppercaseChar = toupper(ch[i]);
        strncat(duresult, &uppercaseChar, 1);
    }
}
 
// (3) 数字降序排序
int digits[10] = {0}; // 存储数字出现的次数
for (int i = 0; ch[i] != '\0'; i++) {
    if (isdigit(ch[i])) {
        digits[ch[i] - '0']++;
    }
}
for (int i = 9; i >= 0; i--) {
    for (int j = 0; j < digits[i]; j++) {
        char digitChar = i + '0';
        strncat(duresult, &digitChar, 1);
    }
}
 
// (4) 特殊符号加倍输出
for (int i = 4; ch[i] != '\0'; i++) {
    if (!isalpha(ch[i]) && !isdigit(ch[i])) {
        strncat(duresult, &ch[i], 1);
        strncat(duresult, &ch[i], 1);
    }
}
 
printf("处理后的字符串:%s\n", duresult);

  

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
printf("hello world, c \n");
printf("你好,中国\n");
char ch[100] = {"GXZXLeaag^*** 1092367145 &*@654123HUYqianrushi" };
int lennum=sizeof(ch)/sizeof(ch[0]);
char *des[4];//={0};
char *gx[0]={{"高训中心"}};
 
int slen=strlen(ch);
char *rdes[slen-4];
printf("%s\n",strRight(rdes,ch,slen-4));
 
printf("%s\n",strLeft(des,ch,4));
printf(des);
if(des=="GXZX")
{
    printf("%s",gx[0]);
}
   

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
printf("hello world, c \n");
printf("你好,中国\n");
char ch[100] = {"GXZXLeaag^*** 1092367145 &*@654123HUYqianrushi" };
int lennum=sizeof(ch)/sizeof(ch[0]);
char *des[8];//={0};
char *gx[0]={{"高训中心"}};
char *newch[100];
printf("%s\n",duReplace(ch,"GXZX","高训中心"));
 
int slen=strlen(ch);
char *rdes[slen-8];
printf("%s\n",strRight(rdes,ch,slen-8));
 
printf("%s\n",strLeft(des,ch,8));
printf(des);
if(des=="GXZX")
{
    printf("%s",gx[0]);
}

  

 

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
printf("hello world, c \n");
 printf("你好,中国\n");
 
 char ch[] = {"GXZXLeaag^*** 1092367145 &*@654123HUYqianrushi" };
 char *dup;
 char newstr[50];
 char *des;
 dup=ch;
 *(dup+14)='d';
 int l=sizeof(ch)/sizeof(ch[0]);
 //倒序显示
 //for(int i=l;i>=0;i--)
 //{
    // printf("%c\n",dup[i]); //
    
// }
 //顺序显示
 for(int i=0;i<l;i++)
 {
 
     if(dup[i]>='A' && dup[i]<='Z')
     {
         printf("%c\n",dup[i]);   
     }
     else if(dup[i]>='a' && dup[i]<='z')
     {
         printf("%c\n",toupper(dup[i]));
         *(dup+i)=toupper(dup[i]);
     }
     else if(isdigit(dup[i]))
     {
         printf("數字:%c\n",dup[i]);
     }
     else
     {
 
         des=newstr;
         //des=dup[i];
         //strcat(des[1],dup[i]);  //strcpy(des, dup[i]);
          //memset(des, '\0', sizeof(des));
         //strcat(&des,&dup[i]);
         *(des+i) = '*';
         *(des+i+1)=dup[i];
         printf("%c\n",dup[i]);
          
     }
      //
      
 }
  printf("%s\n",dup);
 //printf("%s",dup);
 //*(p+10)="G";
 char str1[14] = "涂聚文";
char str2[14] = "google";
char str3[14];
int  len ;
 
/* 复制 str1 到 str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) :  %s\n", str3 );
 
/* 连接 str1 和 str2 */
strcat( str1, str2);
printf("strcat( str1, str2):   %s\n", str1 );
 
/* 连接后,str1 的总长度 */
len = strlen(str1);
printf("strlen(str1) :  %d\n", len );

  

 

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
printf("hello world, c \n");
   printf("你好,中国\n");
 
 
 
   char *dustr = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //这个不可以用替换函数,需要索引长度大于自身的宽试
   char *substr = "1092367145";
   char *substr2 = "654123";
   char geovindu[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //必须索引值大,否则不可以替换
   char *newdig[10];
   char *digg;
   char *sdu;
   char *newdig2[6];
 
 
 
 
   char newstrd[10];
   char *ddu[10];
   int digitsdu[10] = {0}; // 存储数字出现的次数
   for (int i = 0; substr[i] != '\0'; i++) {
       if (isdigit(substr[i])) {
           digitsdu[substr[i] - '0']++;
       }
   }
   for (int i = 9; i >= 0; i--) {
       for (int j = 0; j < digitsdu[i]; j++) {
           char digitChar = i + '0';
           strncat(newstrd, &digitChar, 1);
           printf("%c",digitChar);
           strncat(ddu, &digitChar, 1);
       }
   }
 
   char newstrd2[10];
   char *ddu2[10];
   int digitsdu2[10] = {0}; // 存储数字出现的次数
   for (int i = 0; substr2[i] != '\0'; i++) {
       if (isdigit(substr2[i])) {
           digitsdu2[substr2[i] - '0']++;
       }
   }
   for (int i = 9; i >= 0; i--) {
       for (int j = 0; j < digitsdu2[i]; j++) {
           char digitChar = i + '0';
           strncat(newstrd2, &digitChar, 1);
           //*(newchar+j)=digitChar;
           //*(newchar+i)=digitChar;
           printf("%c",digitChar);
           //ddu2[i]=digitChar;
           strncat(ddu2, &digitChar, 1);
       }
   }
 
   printf("\n1: %s\n",newstrd);
   printf("2: %s\n",ddu);
   printf("3: %s\n",newstrd2);
   printf("4: %s\n",ddu2);
 
 
 
 
   char *newddd=newstrd;
   char *fff;
   char *kk;
   digg=duSortAsc(substr,newdig,10);
   kk=duSortAsc2(substr2,newdig2,6);
 
   fff=duReplace(geovindu,substr,ddu);
   printf("fff:%s\n",fff);
   fff=duReplace(fff,substr2,kk); 
 
   printf("\nnew:%s\n",digg);
   printf("newdig:%s\n",newdig);
   printf("newdig2:%s\n",newdig2);
   printf("kk:%s\n",kk);
   printf("fff:%s\n",fff);
 
 
 
   //查找索引
   char *dus =strstr(dustr,substr); //
   char *dus2=strstr(dustr,substr2);//
   if(dus==NULL)
       printf("can't find %s in %s\n",substr,dustr);
   else
       printf("%s include %s;show the string from start found address:%s\n", dustr,substr,dus);
    //起始索引  
    int index=dus - dustr;
    int index2=dus2-dustr;
    //结束索引
    int endindex=index+strlen(substr);
    int endindex2=index2+strlen(substr2);
    printf("1092367145 start index:%d,end:%d.\n",index,endindex);
    printf("654123 start index:%d,end:%d.\n",index2,endindex2);  
 
   char ch[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi";
   char duresult[100]; // 存储处理后的结果
 
   char gx[]="GXZX";
   
   char *dup;
   char *gxdup;
   char newstr[100];
   char *des;
   dup=fff;
   char gxz[]="高训中心";
   //strcat(newstr, "高训中心");   
   strncat(newstr,&gxz,8); 
   //printf("%s/n",gxdup);
   int gxlen=strlen("高训中心");
   //*(dup+14)='d';
   int l=sizeof(ch)/sizeof(ch[0]);
   //倒序显示
   //for(int i=l;i>=0;i--)
   //{
       // printf("%c\n",dup[i]); //
        
   // }
   //顺序显示
   for(int i=4;i<l;i++)
   {
    
       if(dup[i]>='A' && dup[i]<='Z')
       {
           printf("%c\n",dup[i]);
           char sd=dup[i];
           strncat(newstr,&sd,1); 
 
       }
       else if(dup[i]>='a' && dup[i]<='z')
       {
           printf("%c\n",toupper(dup[i]));
           *(dup+i)=toupper(dup[i]);
           char sx=toupper(dup[i]);
            strncat(newstr,&sx,1);              
       }
       else if(isdigit(dup[i]))
       {
           printf("數字:%c\n",dup[i]);
           char sszi=dup[i];
           strncat(newstr,&sszi,1);  
       }
       else
       {
    
           //des=newstr;
           //des=dup[i];
           //strcat(des[1],dup[i]);  //strcpy(des, dup[i]);
           //memset(des, '\0', sizeof(des));
           //strcat(&des,&dup[i]);
           //*(des+i) = '*';
           //*(des+i+1)=dup[i];
           //printf("%c\n",dup[i]);
           strncat(newstr, &ch[i], 1);
           strncat(newstr, &ch[i], 1);
       }
       //
        
   }
   gxdup=duReplace(ch,"GXZX","高训中心");
   printf("gx=%s\n",gxdup);
   printf("newstr=  %s\n",newstr);
   // (1) 将GXZX前四个字符串中的大写字母转换成“高训中心”
   for (int i = 0; i < 4; i++) {
       if (isupper(ch[i])) {
           strcat(duresult, "高训中心");
       } else {
           // (2) 将字符串中其余的小写字母转换成大写字母
           char uppercaseChar = toupper(ch[i]);
           strncat(duresult, &uppercaseChar, 1);
       }
   }
 
   // (3) 数字降序排序
   int digits[10] = {0}; // 存储数字出现的次数
   for (int i = 0; ch[i] != '\0'; i++) {
       if (isdigit(ch[i])) {
           digits[ch[i] - '0']++;
       }
   }
   for (int i = 9; i >= 0; i--) {
       for (int j = 0; j < digits[i]; j++) {
           char digitChar = i + '0';
           strncat(duresult, &digitChar, 1);
       }
   }
 
   // (4) 特殊符号加倍输出
   for (int i = 4; ch[i] != '\0'; i++) {
       if (!isalpha(ch[i]) && !isdigit(ch[i])) {
           strncat(duresult, &ch[i], 1);
           strncat(duresult, &ch[i], 1);
       }
   }
 
   printf("处理后的字符串:%s\n", duresult);

  

 

 

 

1
2
3
4
5
6
7
8
9
10
//分割字符串   https://githubmota.github.io/2017/12/29/2017-12-29-Linux-C-Split/
char ssss[] = "hello, world! welcome to china!"
char delim[] = " ,!"
 
char *token; 
for(token = strtok(ssss, delim); token != NULL; token = strtok(NULL, delim)) { 
    printf(token); 
    printf("+"); 
printf("\n"); 

  

 

https://github.com/huawenyu/Design-Patterns-in-C
https://swedishembedded.com/design-patterns/
https://blog.csdn.net/ZCShouCSDN/article/details/80217199
https://www.sciencedirect.com/book/9781856177078/design-patterns-for-embedded-systems-in-c
https://www.oreilly.com/library/view/design-patterns-for/9781856177078/
https://github.com/topics/design-patterns?l=c

安装插件“Doxygen Documentation Generator”,用来生成注释。
安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等。或 KoroFileHeader
C/C++ Snippets插件设置

 

posted @   ®Geovin Du Dream Park™  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-16 Java: null object Pattern
2022-10-16 javascript: Webcam
< 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
点击右上角即可分享
微信分享提示