首页 > 其他分享 >C语言学习:字符串的长度与比较

C语言学习:字符串的长度与比较

时间:2023-02-11 21:34:15浏览次数:46  
标签:high INT C语言 char PRINT low 字符串 长度 include

 

 1 #include <io_utils.h>
 2 #include <stdlib.h>
 3 #include <errno.h>
 4 #include <time.h>
 5 #include <string.h>
 6 
 7 void SwapString(char * *first, char * *second) {
 8   char * temp = *first;
 9   *first = *second;
10   *second = temp;
11 }
12 
13 void Shuffle(char * *array, int length) {
14   srand(time(NULL));
15 
16   for (int i = length - 1; i > 0; --i) {
17     int random_number = rand() % i;
18     SwapString(array + i, array + random_number);
19   }
20 }
21 
22 char * *Partition(char * *low, char * *high) {
23   char * pivot = *(low + (high - low) / 2);
24   char * *p = low;
25   char * *q = high;
26 
27   while (1) {
28     while (strcmp(*p,pivot) < 0) p++;
29     while (strcmp(*q, pivot) > 0) q--;
30 
31     if (p >= q) break;
32     SwapString(p, q);
33   }
34 
35   return q;
36 }
37 
38 void QuickSort(char * *low, char * *high) {
39   if (low >= high) return;
40   char * *partition = Partition(low, high);
41   QuickSort(low, partition - 1);
42   QuickSort(partition + 1, high);
43 }
44 
45 int main() {
46   char *string = "Hello World!";
47   PRINT_INT(strlen(string));
48   //PRINT_INT(strnlen_s(string, 100)); // C11, msvc
49   //PRINT_INT(strnlen(string, 100)); // gcc
50 
51   char *left = "Hello World!";
52   char *right = "Hello C Programmers!";
53 
54   PRINT_INT(strcmp(left, right));
55   PRINT_INT(strncmp(left, right, 5));
56 
57   // int array[];
58   char *names[] = {
59       "Cindy",
60       "Don",
61       "Andrey",
62       "Elsa",
63       "George",
64       "Frank",
65       "Benny",
66   };
67 
68   QuickSort(names, names + 6);
69   PRINT_ARRAY("%s, ", names, 7);
70   return 0;
71 }
View Code

 

标签:high,INT,C语言,char,PRINT,low,字符串,长度,include
From: https://www.cnblogs.com/liumy/p/17112612.html

相关文章