与冒泡排序算法对字符串进行排序 [英] sorting strings with bubble sort algorithm
问题描述
这code存储在一个整数变量字和词语的一个multidimensionnel字符串的编号,然后按字母顺序使用排序功能的话。我的问题是在函数调用。
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
无效的排序(字符* [50],INT);
诠释的main()
{
INT I,N = 0;
个char [50] [10]; scanf的(%d个,&放大器; N); // scaning单词的数目 对于(i = 0; I< = N;我++)// scaning的话
得到(S [I]); 排序(S,N); 对于(i = 0; I< = N;我++)//打印文字整理
的printf(%S \\ N,S [I]);
}
无效的排序(字符* S [50],INT N)
{
INT I,J,CMP;
炭TMP [1] [10]; //话冒泡排序
对于(i = 0; I< N;我++)
为(J = 0; J< N-1; J ++)
{
CMP = STRCMP(S [J],S [D + 1]); 如果(CMP大于0)
{
的strcpy(TMP [0],S [J + 1]);
的strcpy(S [D + 1],S [J]);
的strcpy(S [J],TMP [0]);
}
}
}
打开你的警告。 个char [50] [50]
是无法转换为的char *
。
试试这个:
无效排序(CHAR(*)[50],INT);
这会告诉你在一个指针传递给50个字符至少有一个缓冲的编译器。
这是多维数组衰变时传入功能。
和进一步摆脱你的愚蠢观念的的char []
相同的char *
,由于某种原因仍在教随处可见,阅读: http://c-faq.com/ aryptr / aryptr2.html
This code stores the number of words in an integer variable and the words in a multidimensionnel string then sorts the words alphabetically using a function. The problem I have is in the function call.
#include<stdio.h>
#include<string.h>
void sort(char*[50],int);
int main ()
{
int i,n=0;
char s[50][10];
scanf("%d",&n);//scaning the number of words
for(i=0;i<=n;i++)//scaning the words
gets(s[i]);
sort(s,n);
for(i=0;i<=n;i++)//printing the words sorted
printf("%s\n",s[i]);
}
void sort(char*s[50],int n)
{
int i,j,cmp;
char tmp[1][10];
//bubble sorting of words
for(i=0; i<n; i++)
for(j=0; j<n-1; j++)
{
cmp=strcmp(s[j],s[j+1]);
if(cmp>0)
{
strcpy(tmp[0],s[j+1]);
strcpy(s[j+1],s[j]);
strcpy(s[j],tmp[0]);
}
}
}
Turn on your warnings. char s[50][50]
is not convertible to char*
.
Try this:
void sort(char(*)[50],int);
This tells the compiler you pass in a pointer to at least one buffer of 50 characters. This is what multidimensional arrays decay to when passed into functions.
And to further rid you of the silly notion that "char[]
is the same as char*
", which for some reason is still taught everywhere, read this: http://c-faq.com/aryptr/aryptr2.html
这篇关于与冒泡排序算法对字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!