求助一道C 语言题!!!!
任意给定一个字符串s,将s中的各字母按照字典顺序排序形成新的字符串t,依次输出t中字符在原字符串s中的前一位如exam 排序后为aemx
a的前一位是x,e的前一位是m ,m的前一位是a, x的前一位是e
最后输出xmae
#include <stdio.h> #include <string.h> #define SIZE 81 #define SWAP(a, b) \ int temp = (a); \ (a) = (b); \ (b) = temp; void select_sort_asc(char * s) { int i, j, k; for(i = 0; i < strlen(s) - 1; i++) { k = i; for(j = i + 1; j < strlen(s); j++) { if(s[j] < s[k]) { k = j; } } if(k != i) { SWAP(s[k], s[i]); } } } char matching(char ch, char * s) { int i = 0; while(s[i]) { if(s[i] == ch) { if(i == 0) { return s[strlen(s) - 1]; } else { return s[i - 1]; } } i++; } } int main(void) { char s[SIZE], t[SIZE], * p = t; gets(s); strcpy(t, s); select_sort_asc(t); while(*p) printf("%c", matching(*p++, s)); puts(""); return 0; }
#include <stdio.h> #include <string.h> void swap(char * i, char * j) { char k = *i; *i = *j; *j = k; } int main() { char s[100] = {0}, si[100][2] = {0}; int i, j, l; printf("Give me a string: "); fflush(stdout); scanf("%s", s); l = strlen(s); for (i = 0; i < l; i++) { si[i][0] = s[i]; si[i][1] = i - 1; } si[0][1] = l - 1; for (i = 0; i < l - 1; i++) { for (j = i + 1; j < l; j++) { if (si[j][0] < si[i][0]) { swap(&si[i][0], &si[j][0]); swap(&si[i][1], &si[j][1]); } } } for (i = 0; i < l; i++) { printf("%c", s[si[i][1]]); } printf("\n"); return 0; }