#includestdio.h
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)確山,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
#includestring.h
void?Sort(char?s[][51],int?n)?{
char?t[51];
int?i,j,k;
for(i?=?0;?i??n?-?1;?++i)?{
k?=?i;
for(j?=?i?+?1;?j??n;?++j)?{
if(strcmp(s[k],s[j])??0)
k?=?j;
}
if(k?!=?i)?{
strcpy(t,s[i]);
strcpy(s[i],s[k]);
strcpy(s[k],t);
}
}
}
int?main()?{
char?s[][51]?=?{"continue","break","for","while","struct","swicth","edit","copy","past","please"};
int?i,n?=?10;
printf("排序前:\n");
for(i?=?0;?i??n;?++i)
printf("%s\n",s[i]);
Sort(s,10);
printf("\n排序后:\n");
for(i?=?0;?i??n;?++i)
printf("%s\n",s[i]);
printf("\n");
return?0;
}
首先你的string函數(shù)的函數(shù)類型沒有指定,先定義為char*類型
同時(shí)你對str[SIZE]數(shù)組的初始化方式得到的每一個(gè)指針的類型為只讀類型的指針(相當(dāng)于加上了const關(guān)鍵字),所以初始化的時(shí)候改為如下這樣的初始化方式吧。
然后main函數(shù)修改為
main(void)
{
char str[SIZE][30]=
{
{"Father"},
{"B"},
{"C"},
{"OK"},
{"Brother"}
};
for (i=0;iSIZE-1;i++)
{
printf("%s\n",string(str[i]));
}
}
我做了一些修改,下面的程序可以運(yùn)行!你肯定是個(gè)初學(xué)者,所以不要急慢慢來,把基礎(chǔ)的東西一定要搞懂.
我所做的修改主要有3點(diǎn):
1、自定義函數(shù)不能在函數(shù)名后加分號(除非在函數(shù)說明時(shí))
2、自定義函數(shù)如要放在主函數(shù)之后,則在主函數(shù)中一定要說明int
opp(int
x);
否則一定要放在主函數(shù)之前定義
3、printf()是格式化的輸出函數(shù),一定要給出輸出格式,你的程序中是int型的所以用“%d”
最后建議你找一本c語言的書好好看看!
#include
int
opp(x)
{
int
r;
r=x+1;
return
(r);
}
main()
{
int
a=1,b;
b=opp(a);
printf("%d",b);
}
/*
排序前:
One-1 Two-2 Three-3 Four-4 Five-5 Six-6 Seven-7 Eight-8 Nine-9 Ten-10
排序后:
Two-2 Three-3 Ten-10 Six-6 Seven-7 One-1 Nine-9 Four-4 Five-5 Eight-8
Press any key to continue
*/
#include stdio.h
#include string.h
void sort(char *a[],int n) { // 選擇排序
char *temp;
int i,j,k;
for(i = 0;i n - 1;i++) {
k = i;
for(j = i + 1;j n;j++)
if(strcmp(a[k],a[j]) 0) k = j;
if(k != i) { // 交換的是字符串的地址,不是字符串的內(nèi)容
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
int main() {
int i,n = 10;
char *s[] = {"One-1","Two-2","Three-3","Four-4","Five-5","Six-6","Seven-7","Eight-8","Nine-9","Ten-10"};
printf("排序前:\n");
for(i = 0;i n;i++) printf("%s ",s[i]);
printf("\n\n");
sort(s,n);
printf("排序后:\n");
for(i = 0;i n;i++) printf("%s ",s[i]);
printf("\n\n");
return 0;
}
void fun(char a[])
{ int k=0,i,j;
char t;
while(a[k])
k++;//計(jì)算字符串長度
for(i=0;ik;i++)//冒泡排序
for(j=0;jk-1-i;j++)
if(a[j]a[j+1])
{t=a[j];
a[j]=a[j+1];
a[j+1]=t;}
}