C语言字符串排序函数指针 字符串排序c++实现

C语言中的字符指针数组排序完整程序如下:
#includestdio.h
void sort_string(char **p,int n){
int i,j;
char s[80];
for (i=0;in-1;i){
for (j=i 1;jn;j){
if (strcmp(*(p i),*(p j))0){
strcpy(s,*(p i));strcpy(*(p i),*(p j));strcpy(*(p j),s);
};};};
}
int main()
{
char *str[]={ "teacher", "student", "maneger", "employee", "telicent" };
int i,n=5;
sort_string(str,n);
for (i=0;in;i) printf("%s\n",str[i]);
return 0;
}
利用指针,编写一个函数,让三个字符串从小到大排序,我这样有什么问题?代码没有用C的string类C语言字符串排序函数指针,操作目标还是C的char 型数组C语言字符串排序函数指针,所以不能用和来判断字符串的大小 , 要用库函数strcmp比较两个字符串的大小 。
拷贝到sort函数中的指针只是实参指针的“值”,所以在函数中改变那些指针的值在函数中有用,效果返回不到主函数中去,就是说在sort中输出结果是有效的 , 在主函数中字符串的大小还是原样子,不会有排序结果 。这问题用3个办法解决:一是就按目前结构写sort,在sort中输出比较结果C语言字符串排序函数指针;二是在sort中通过指针直接交换主函数中的数组内容;三是有网友提出的用二级指针来交换主函数中的指针 。
如果用C的string类 , 那就十分简单了,操作字符串就像操作普通变量一样 。
下面提供一个拷贝数组内容的代码供参考,并可续问 。
代码文本:
//#include "stdafx.h"//vc6.0? Maybe should add this line.
#include string
#include iostream
【C语言字符串排序函数指针 字符串排序c 实现】using namespace std;
int main(int argc,char *argv[]){
void sort(char*,char*,char*);
char s1[255],s2[255],s3[255];
//char *p1,*p2,*p3;
cout"Enter the 3 strings(Separated by ' ')...\n";
cins1s2s3;
//p1=s1;p2=s2;p3=s3;
sort(s1,s2,s3);
cout"After the sorting:\n";
couts1endls2endls3endl;
return 0;
}
void sort(char *p1,char *p2,char *p3){
char r[255],t;
if((t=strcmp(p1,p2))0)
strcpy(r,p1),strcpy(p1,p2),strcpy(p2,r);
if((t=strcmp(p1,p3))0)
strcpy(r,p1),strcpy(p1,p3),strcpy(p3,r);
if((t=strcmp(p2,p3))0)
strcpy(r,p2),strcpy(p2,p3),strcpy(p3,r);
}
c语言如何用指针排列字符#include stdio.h
#include string.h
void sort_str(char *str) { // 选择排序
//待补全部分
int i,j,k,t,len = strlen(str);
for(i = 0; ilen - 1;i) {
k = i;
for(j = i1; jlen;j) {
if(str[k]str[j])
k = j;
}
if(k != i) {
t = str[k];
str[k] = str[i];
str[i] = t;
}
}
}
int main() {
char str[101];
scanf("%s", str);
sort_str(str);//对字符串进行排序
printf("%s\n", str);
return 0;
}
字符串数组排序(C语言)(qsort库函数)声明一个字符串指针数组存放每个字符串的首地址 , 调用库函数qusort按题目要求对字符串指针排序,不移动源字符串 。关键是要设计一个好的比较函数,精巧地解决“按长度、长度相等时按大小”排序的问题 。举例代码如下:
//#include "stdafx.h"//If the vc6.0, with this line.
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define N 10//字符串个数
#define LN 21//限制字符串长度为20
int mycmp(const void *a,const void *b){//比较函数
char *pa=*(char **)a,*pb=*(char **)b;
int x=int(strlen(pa)-strlen(pb));//依长度比较
return x ? x : strcmp(pa,pb);//长度相等时依大小比较
}
int main(void){
int i=0,j=0;
char *f[N],w[LN*N];//声明指针数组f和字符串总空间
printf("Input %d string(s)(length=%d)...\n",N,LN);
while(iN){//输入并将字符串首址赋给f[i]
if(scanf(" %[1234567890]",f[i]=w j)0strlen(f[i])LN)
i,j =LN;
else printf("Error, redo: Required length less than %d:",LN);
}
qsort(f,N,sizeof(char *),mycmp);//调用库函数对字符串指针排序
for(i=0;iN;printf("%s\n",f[i]));//输出...
return 0;
}
采用指针对数组进行排序c语言指针/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include stdio.h
#include stdlib.h
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char *resort(char *a)
{
char cMin, cSwap;
int i, j, iIndex, iSize = strlen(a);
for(i=0; iiSize; i)
{
cMin = 127;
for(j=i; jiSize; j)
{
if(a[j] = cMin)
{
cMin = a[j];
iIndex = j;
}
}
cSwap = a[i];
a[i] = cMin;
a[iIndex] = cSwap;
}
return a;
}
int main(int argc, char *argv[])
{
char a[100];
gets(a);
puts(resort(a));
return 0;
}
C语言:从键盘输入长度为10的字符串,除第一和最后一个字符外按降序排序,编写函数指针传递完成,咋写?代码文本:
#include "stdio.h"
#define N 10
void myf(char *p){
char i,j,k,n;
for(n=0;p[n];n);
for(n--,i=1;in-1;i){
for(j=(k=i) 1;jn;j)
if(p[k]p[j])
k=j;
if(k!=i)
j=p[k],p[k]=p[i],p[i]=j;
}
}
int main(int argc,char *argv[]){
char s[N 1];
printf("Enter a string(length 10)...\n");
scanf("s",s);
printf("After ordering:\n");
myf(s);
puts(s);
return 0;
}
关于C语言字符串排序函数指针和字符串排序c实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读