One day (or day! A few foreign friends to visit the elder brother, but sound elder brother once can't meet so many people at the same time, so he wanted to put the names of all the foreign friends (Jane, there is only one word, for example: Mike) according to the dictionary order, in order to determine the order of meet,
Enter
Each test case input to a line, including a number of foreign friends Jane (each Jane name only a word, and not more than 10 letters), is no more than 100 foreign friends to visit brother,
O
In dictionary order of foreign friends Jane sorting, output the result of the sort, name with a space between phase separation, each output to a line,
The sample input
Michael KangKang HangMeiMei LiLei
Sample output
HangMeiMei KangKang LiLei Michael
My code is as follows:
#include
#include
#include
Void sort (char * a []);
Void the print (char * a []);
Int main ()
{
Char * a, [100].
Int k, n;
For (k=0; K & lt; 100; K++)
{
A [k]=(char *) malloc (sizeof (char));
The scanf (" % s ", a [k]);
}
N=strlen (a);
sort(a);
Print (a);
return 0;
}
Void sort (char * a [])
{
Int I, j, n.
Char * temp;
N=strlen (a);
for(i=0; i
for(j=0; J
If (STRCMP (a [j], a [m + 1]) & gt; 0)
{
Temp=a, [j].
A [j]=a, a + 1 bonus to [j].
A=\ [j + 1];
}
}
}
}
Void the print (char * a [])
{
Int I, n;
N=strlen (a);
for(i=0; i
Printf (" % s ", a [I]);
}
}
Want to ask what's wrong with this code and if this has multiple input sample how do I modify the code
CodePudding user response:
Modeled after what you thinking, contrast see#include
#include
#include
//new splite function, the input string into the separate string of names one by one, and return to a array of, the number of names
Void splite (char * buffer, char * a [100], int * pN);
Void sort (char * a [], int n);//added a parameter n, how many of said array personal name
Void the print (char * a [], int n);//added a parameter n, ditto
Int main ()
{
Char * a, [100].//up to 100 people
Char buffer [1200].//used to receive input, 100 * 11 characters (including Spaces) behind every=1100; Define some prevention more
Gets (buffer);
int n;
Splite (buffer, a, & amp; N);
Sort (a, n);
Print (a, n);
//the complete code, there should be added a function, release the memory in the array a. the malloc out
return 0;
}
//the original code, you changed, n from the parameters passed in the
Void sort (char * a [], int n)
{
int i,j;
Char * temp;
for(i=0; i
for(j=0; J
If (STRCMP (a [j], a [m + 1]) & gt; 0)
{
Temp=a, [j].
A [j]=a, a + 1 bonus to [j].
A=\ [j + 1];
}
}
}
}
//you are the original code, n from parameter
Void the print (char * a [], int n)
{
int i;
for(i=0; i
Printf (" % s ", a [I]);
}
}
//multiple names, separated with a space of buffer are disassembled to char * a [], and returns the number of into * pN
Void splite (char * buffer, char * a [100], int * pN) {
Int count=0;
Int col=0;
int i=0;
Int len=(int) strlen (buffer);
Char \ [11].
for (i=0; i
\ [col++]=buffer [I];
{} else if (col)
Char * p=(char *) malloc (col + 1);
\ [col]=0;
Strcpy (p, temp);
A [count++]=p;
Col=0;
}
}
If (col) {
Char * p=(char *) malloc (col + 1);
\ [col]=0;
Strcpy (p, temp);
A [count++]=p;
Col=0;
}
* pN=count;
}
CodePudding user response:
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.htmlI hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html