Enter 10 equal-length strings in the main function and sort them with another function. These 10 ordered strings are then output in the main function.
It is done in two ways.
Method One: Using two-dimensional array to do function parameters;
Method Two: Use pointers to one-dimensional arrays for function parameters.
Method 1:2-dimensional array:
#include <iostream>
#include <string.h>
using namespace Std;
int main ()
{
Char str[10][20];
cout<< "input strings in the same length:" <<endl;
for (int i=0;i<10;i++) cin>>str[i];
void sort (char [10][20]);
Sort (str);
cout<< "Now the sorted strings is:" <<endl;
void print (char [10][20]);
Print (str);
return 0;
}
void sort (char a[10][20])
{
for (int i=0;i<10;i++)
{int K=i;char t[20];
for (int j=i;j<10;j++)
if (strcmp (A[j],a[k]) <0)
{
strcpy (T,a[j]); strcpy (A[j],a[k]); strcpy (a[k],t);
}
}
}
void print (char a[10][20])
{
for (int j=0;j<10;j++) cout<<a[j]<<endl;
}
Operation Result:
Method Two: Use pointers to one-dimensional arrays for function parameters:
#include <iostream>
#include <string.h>
using namespace Std;
int main ()
{
char (*p) [10];char ch[10][10];
cout<< "Input strings:" <<endl;
for (int i=0;i<10www.furongpt.com;i++) cin>>ch[i];
P=ch;
void Sort (char (*) [10]);
Sort (p);
cout<< "Now the Strings is:" <<endl;
for (int j=0;j<10;j++) cout<<p[j]<<endl;
return 0;
}
void sort (char www.furong157.com (*P) [10])
{
for (int i=0;i<10;i++)
{int K=i;char t[10];
for (int j=i;j<10;j++)
if (strcmp (P[j],p[k]) www.567860.cn <0)
{
strcpy (T,p[j]); strcpy (P[j],p[k]); strcpy (p[k],t);
}
C + + string ordering