The use of dynamic arrays, sorted by city name spelling

#include <the iostream>
#include <stdlib.h>
#include <string.h>
the using namespace STD;
void INPUT (* City char [], int n-);
void Sort (char * City [], int n-);
int main ()
{
int I = 0, m;
COUT << "enter city number:" << endl;
CIN >> m;
char ** city new new char * = [m]; // pointer generated city, point array city [m], city [i ] point to the name of each city

input (city, m); // m Enter city name, and a pointer to the first address of the
sort (city, m); // Sort sort function
cout << "arranged in alphabetical city output follows: \ n-";
for (I = 0; I <m; I ++)
COUT << city [i] << endl; // with the same primer solutions and an array of pointers embodiment mode effects, i.e., the output * (city + i) and the output city [i] of the same effect, is the output of the entire string
return 0;
}
void input (char * City [], int n-) // type function input parameters received city [i] the first address of the array,
{
char STR [20 is];
int i;
cout << "enter" << n << "names of cities (please use the Pinyin): \ the n-";
// Start
for (i = 0; i <the n-; i ++)
{
cin >> str;
city [i] = (char * ) malloc (sizeof (char) * (strlen (str) +1)); // dynamic array applications, with city [i] point, such as city [0] is stored in a first city name
strcpy (city [I], STR);
}

}

 

Start //
// array to store the name of the city city sort by function strcmp

void sort(char *city[],int n)
{
char *temp;
int i,j;
for(i=0;i<n;i++)
{
for(j=1;j<n-1;j++)
{
if(strcmp(city[j],city[j-1])<0)
{
temp=city[j-1];
city[j-1]=city[j];
city[j]=temp;
}
}
}
//end
}

Guess you like

Origin www.cnblogs.com/cloverlp/p/12081531.html