Creates an array of strings, a total of 5 elements, each of which holds up to 30 characters, and writes a function to sort the entire array.

Source: Internet
Author: User
Tags strcmp

First type: (similar to bubble sort) #include <stdio.h> #include <stdlib.h> #include <string.h>int main () {char a[5][30]={" AAA "," BBB "," CCC "," adb "," DEGF "};int i=0;int j=0;char b[30];for (i=0;i<4;i++) {for (j=0;j<4-i;j++) {if (strcmp (a[j ],a[j+1] >0) {strcpy (b,a[j]); strcpy (a[j],a[j+1)); strcpy (a[j+1],b);}} for (i=0;i<5;i++) {printf ("%s\n", A[i]);} System ("pause"); return 0;} Second: fast-#include<stdio.h> #include <stdlib.h> #include <string.h>int my_strcmp (const void* P,const    void* q) {/*char* a= (char*) p; char* b= (char*) q;*/if (strcmp ((char*) p, (char *) q) ==0) {return 0;} else if (strcmp (char*) p, (char *) q) >0) return 1;elsereturn-1; int main () {char a[5][30]={"AAA", "BBB", "Bdv", "FGH", "DDS"};int I;qsort (A,sizeof (a)/sizeof (a[0]), sizeof (A[0]), My_ strcmp); for (i=0;i<5;i++) {printf ("%s\n", A[i]);} System ("pause"); return 0;}

This article from "Liveyoung" blog, reproduced please contact the author!

Creates an array of strings, a total of 5 elements, each of which holds up to 30 characters, and writes a function to sort the entire array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.