我有一個字符數組快速排序的字符(串)C編程的一個數組
char word[30];
,保持一個字,用戶將輸入和我想要的字母 例如排序,如果這個詞是「cat」 我想讓它變成「act」 我認爲這是一項相當簡單的任務,但作爲C編程的初學者,我發現互聯網上的例子相當混亂。
這是我的代碼試圖做冒泡排序...
還不行
#include <stdio.h>
#include<string.h>
#define MAX_STRING_LEN 30
main()
{
char w1[30], w2[30];
char tempw1[30], tempw2[30];
int n,i,k;
char temp;
printf("Give the first word: ");
scanf("%s",&w1);
printf("Give the second word: ");
scanf("%s",&w2);
if(strlen(w1)==strlen(w2)) /* checks if words has the same length */
{
strcpy(tempw1,w1); /*antigrafei to wi string sto tempw1 */
strcpy(tempw2,w2); /*antigrafei to w2 string sto tempw2 */
n=strlen(w1);
for (i=1; i<n-1; i++)
{
for (k=n;k>i+1;k--)
{
if (w1[k] < w1[k-1])
{
temp=w1[k-1];
w1[k-1]=w1[k];
w1[k]=temp;
}
}
}
for (i=1; i<n-1; i++)
{
for (k=n;k>i+1;k--)
{
if (w2[k] < w2[k-1])
{
temp=w2[k-1];
w2[k-1]=w2[k];
w2[k]=temp;
}
}
}
printf("%s \n",tempw1);
printf("%s \n",w1);
printf("%s \n",tempw2);
printf("%s \n",w2);
/* call qsort */
/* call compare */
}
else printf(" \n H lexh %s den einai anagrammatismos tis lexhs %s",w1,w2);
return 0;St
的容易排序算法之一是冒泡排序。 – dutt 2013-02-16 14:11:29
好的,謝謝你的氣泡排序建議我認爲這隻會在整數工作!我試圖馬上嘗試它,我試圖工作的程序是一個字典遊戲作爲excersise我已經解決了所有其他部分,我比較這兩個數組,但需要先對它們進行排序來比較。 – poseidon11 2013-02-16 14:19:17
@ poseidon11 1.字符**是**整數。 2.你可以想出一些算法來比較基於它們屬性的對象,否則就不會有通用的排序算法。 – 2013-02-16 14:22:01