horselurrver

Ch11 Ex10--needs work

Aug 11th, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define SIZE 50
  6. #define LIM 2
  7. int count=0;
  8.  
  9. void printlist(char array[LIM][SIZE]);
  10. void stsrt(char *strings[], int num);
  11. void firstword(char array[LIM][SIZE]);
  12. int main(void){
  13.     int k;
  14.     char ch;
  15.     char array[LIM][SIZE];
  16.     char *ptstr[LIM]; //array of pointer variables
  17.     printf("Input up to %d lines.\n", LIM);
  18.     printf("To stop, press the Enter key at a line's start.\n");
  19.     while(count<LIM && gets(array[count])!=NULL && array[count][0]!='\0'){
  20.         ptstr[count]=array[count];
  21.         count++;
  22.     }
  23.    
  24.    /* while((ch=getchar())!='q'){
  25.         printf("Enter the option.\n");
  26.         printf("a.Print original list of strings.\n");
  27.         printf("b.Print the strings in ASCII collating sequence.\n");
  28.         printf("c.Print in increasing length.\n");
  29.         printf("d.Print in order of length of first word.\n");
  30.         printf("q.Quit.\n");
  31.         if(ch=='a')
  32.             printlist(array);
  33.         else if(ch=='c'){
  34.             stsrt(ptstr, count);
  35.             for(k=0; k<count; k++)
  36.                 puts(ptstr[k]);
  37.         }
  38.         else if(ch=='d')
  39.             firstword(array);
  40.        
  41.     }
  42.    
  43. */
  44.     printlist(array);
  45.     stsrt(ptstr, count);
  46.     for(k=0; k<count; k++)
  47.         puts(ptstr[k]);
  48.     firstword(array);
  49.     return 0;
  50. }
  51.  
  52. void printlist(char array[LIM][SIZE]){
  53.     int x;
  54.     for(x=0; x<count; x++){
  55.         printf("%d.%s\n", x+1, array[x]);
  56.     }
  57. }
  58.  
  59. void firstword(char array[LIM][SIZE]){//doesn't work at all
  60.     int x, y=0;
  61.     int first_len=0;
  62.     for(x=0; x<count; x++){
  63.         if((y+1)==(int)strlen(array[x])){
  64.             printf("hi\n");
  65.             first_len=(int)strlen(array[x]);
  66.            
  67.         }
  68.         else{
  69.             while(!isspace(array[x][y])){
  70.                 y++;
  71.                 first_len++;
  72.                 printf("length is now %d\n", first_len);
  73.                 }
  74.             }
  75.             printf("%s\n", array[x]);
  76.             printf("%d\n", first_len);
  77.             first_len=0;
  78.         }
  79.    
  80.     }
  81.        
  82.  
  83. void stsrt(char *strings[], int num){//sort strings by length
  84.     char *temp;
  85.     int top, seek;
  86.    
  87.     for(top=0; top<num-1; top++){
  88.         for(seek=top+1; seek<num; seek++){
  89.             if((strlen(strings[seek]))<(strlen(strings[top]))){
  90.                 temp=strings[top];
  91.                 strings[top]=strings[seek];
  92.                 strings[seek]=temp;
  93.             }
  94.         }
  95.     }
  96. }
Add Comment
Please, Sign In to add comment