Input 10 unequal strings in the main function, use another function to sort them, and then the main function outputs these 10 sorted strings!

Source: Internet
Author: User

// Process with a pointer Array

# Include <stdio. h>
# Include <string. h>
Int main ()
{
Void sort (char * []);
Int I;
Char STR [10] [6], * P [10];
Printf ("Please input 10 string:/N ");
For (I = 0; I <10; I ++) // first assign the first address of 10 STR to 10 p [I];
P [I] = STR [I]; // assign the first address of the string I to the I element of the array P;
For (I = 0; I <10; I ++)
Scanf ("% s", P [I]); // enter it in & P [I]
Sort (P );
Printf ("the output 10 string:/N ");
For (I = 0; I <10; I ++)
Printf ("% s/n", P [I]); // output to P [I];
}
Void sort (char * s [])
{
Char * temp;
Int I, J;
For (I = 0; I <9; I ++)
For (j = 0; j <9-I; j ++)
If (strcmp (* (S + J), * (S + J + 1)> 0)
{
Temp = * (S + J); // * (S + J) points to the array pointer. I think it should be the first address of the string. Therefore, you can assign a value to the temp pointer directly;
* (S + J) = * (S + J + 1 );
* (S + J + 1) = temp;
}
}

// Two-dimensional array of struct type
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Void sort (char s [] [6]);
Char STR [10] [6];
Int I;
Printf ("Please input string:/N ");
For (I = 0; I <10; I ++)
Scanf ("% s", & STR [I]); // STR is a pointer to a one-dimensional array consisting of six elements
Sort (STR); // STR is a pointer to a one-dimensional array consisting of six elements.
Printf ("Please output string:/N ");
For (I = 0; I <10; I ++)
{
Printf ("% s", STR [I]); // STR is a pointer to a one-dimensional array consisting of six elements
Printf ("/N ");
}
Return 0;
}
Void sort (char s [10] [6]) // The form parameter S is a pointer to a one-dimensional array consisting of six elements
{
Char * P, temp [10];
Int I, J;
P = temp;
For (I = 0; I <10; I ++)
For (j = 0; j <10-i; j ++)
If (strcmp (s [J], s [J + 1])> 0) // The format error is not strcmp (s [J]> S [J + 1]); after comparison
{
Strcpy (P, S [J]);
Strcpy (s [J], s [J + 1]);
Strcpy (s [J + 1], P );
}
}

 

Use a pointer pointing to a one-dimensional array as a function parameter.

# Include <stdio. h>
# Include <string. h>
Int main ()
{
Void sort (char (* s) [6]); // a one-dimensional array pointer as a function parameter
Int I;
Char STR [10] [6];
Char (* P) [6]; // defines the pointer of a one-dimensional array as a function parameter.
Printf ("Please input string:/N ");
For (I = 0; I <10; I ++)
Scanf ("% s", & STR [I]);
P = STR; // assign the STR one-dimensional array pointer to P;
Sort (P );
Printf ("the output sequence:/N ");
For (I = 0; I <10; I ++)
Printf ("% s/n", STR [I]);
Return 0;
}
Void sort (char (* s) [6]) // calls a pointer to a one-dimensional array as a function parameter;
{
Int I, J;
Char temp [6], * t;
T = temp;
For (I = 0; I <9; I ++) // I should be smaller than 9; if it is smaller than 10, it is compared 9 + 1; according to the bubble rule,
For (j = 0; j <9-I; j ++) // nine times are required for the first comparison, that is, nine times from I = 0 to I = 8; eight times are required for the second comparison; and so on;
If (strcmp (s [J], s [J + 1])> 0)
{
Strcpy (t, s [J]);
Strcpy (s [J], s [J + 1]);
Strcpy (s [J + 1], t );
}
}

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.