#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int menu(char *str_array);
int print_origin(char **str_array);
int print_ascii(char **str_array);
int print_length(char **str_array);
int print_first_word(char **str_array);
char * customizeget_s(char * row,int buffer);
unsigned int wordlength(char *str);
int main()
{
char stringstorge[10][40];
char *str_array[10];
int count=0; //As for count
while(count<10 && customizeget_s(stringstorge[count],40)!= NULL &&
stringstorge[count][0]!='\0')
{
str_array[count]=stringstorge[count];
count++;
}
while (menu(str_array))
{
printf("**********************************************************\n");
}
return 0;
}
int menu(char *str_array)
{
printf("\nPlease select a option by enter a number:\n");
printf("1:print the original strings.\n");
printf("2:print the strings by ASCII\n");
printf("3:print the strings by length\n");
printf("4:print the strings by the first word length\n");
printf("5:quit\n");
int selectop;
while ( scanf("%d",&selectop)!=1)
{
printf("ERROR num");
getchar();
printf("\n Input again:\n");
}
switch (selectop)
{
case 1:print_origin(str_array);break;
case 2:print_ascii(str_array);break;
case 3:print_length(str_array);break;
case 4:print_first_word(str_array);break;
case 5:return 0;
default :printf("ERROR must be a number between 1-5.\n");break;
}
return 1;
}
char * customizeget_s(char * row,int buffer)
{
char * return_p1;
int i=0;
return_p1=fgets(row,buffer,stdin);
if (return_p1)
{
while (row[i]!='\n' && row[i]!='\0')
i++;
if (row[i]=='\n')
row[i]='\0';
else
while (getchar()!='\n')
continue;
}
return return_p1;
}
int print_origin(char **str_array)
{
int i=0;
for (;i<10 && *str_array!=NULL;i++ )
{
puts(*(str_array+i));
}
return 0;
}
int print_ascii(char **str_array)
{
int i,j;char *temp;
for (i=0;i<10 && *(str_array+i)!=NULL;i++)
{
for (j=i;j<10 && *(str_array+i)!=NULL;j++)
{
if (**(str_array+j)>**(str_array+i))
{
temp=*(str_array+i);
*(str_array+i)=*(str_array+j);
*(str_array+j)=temp;
}
}
}
for (i=0;i<10 && **(str_array+i) != '\0' ;i++)
{
puts(*(str_array+i));
}
return 0;
}
int print_length(char **str_array)
{
int i,j;char *temp;
for (i=0;i<10 && *(str_array+i)!=NULL;i++)
{
for (j=i;j<10 && *(str_array+i)!=NULL;j++)
{
if ( strlen(*(str_array+j)) > strlen(*(str_array+i)) )
{
temp=*(str_array+i);
*(str_array+i)=*(str_array+j);
*(str_array+j)=temp;
}
}
}
for (i=0;i<10 && **(str_array+i) != '\0' ;i++)
{
puts(*(str_array+i));
}
return 0;
}
int print_first_word(char **str_array)
{
int i,j;char *temp;
for (i=0;i<10 && *(str_array+i)!=NULL;i++)
{
for (j=i;j<10 && *(str_array+i)!=NULL;j++)
{
if ( wordlength(*(str_array+j)) > wordlength(*(str_array+i)) )
{
temp=*(str_array+i);
*(str_array+i)=*(str_array+j);
*(str_array+j)=temp;
}
}
}
for (i=0;i<10 && **(str_array+i) != '\0' ;i++)
{
puts(*(str_array+i));
}
return 0;
}
unsigned int wordlength(char *str)
{
unsigned int i;
for (i=0;i<40 && ( *(str+i)!=' ' ) && (*(str+i)!='\0') ;i++)
;
return i;
}
c primer plus十一章练习题第11题个人的答案
采用了对指针数组进行插入排序的方法。