输入字符串:c++:1,python:3,c:5,java:4,rust:2 请将其中的字符串按照升序排序输出:(输出的格式按照逗号隔开) c,c++,java,python,rust请用C语言实现代码
时间: 2023-08-10 15:03:42 浏览: 109
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50
typedef struct {
char name[MAX_LEN];
int count;
} Language;
int compare(const void *a, const void *b) {
Language *la = (Language *)a;
Language *lb = (Language *)b;
return strcmp(la->name, lb->name);
}
int main() {
char str[] = "c++:1,python:3,c:5,java:4,rust:2";
Language languages[5];
char *token = strtok(str, ",");
int i = 0;
while (token != NULL) {
char *colon = strchr(token, ':');
strncpy(languages[i].name, token, colon - token);
languages[i].name[colon - token] = '\0';
languages[i].count = atoi(colon + 1);
token = strtok(NULL, ",");
i++;
}
qsort(languages, 5, sizeof(Language), compare);
for (i = 0; i < 5; i++) {
printf("%s,", languages[i].name);
}
return 0;
}
```
阅读全文