Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #define SIZE 50
- #define LIM 2
- int count=0;
- void printlist(char array[LIM][SIZE]);
- void stsrt(char *strings[], int num);
- void firstword(char array[LIM][SIZE]);
- int main(void){
- int k;
- char ch;
- char array[LIM][SIZE];
- char *ptstr[LIM]; //array of pointer variables
- printf("Input up to %d lines.\n", LIM);
- printf("To stop, press the Enter key at a line's start.\n");
- while(count<LIM && gets(array[count])!=NULL && array[count][0]!='\0'){
- ptstr[count]=array[count];
- count++;
- }
- /* while((ch=getchar())!='q'){
- printf("Enter the option.\n");
- printf("a.Print original list of strings.\n");
- printf("b.Print the strings in ASCII collating sequence.\n");
- printf("c.Print in increasing length.\n");
- printf("d.Print in order of length of first word.\n");
- printf("q.Quit.\n");
- if(ch=='a')
- printlist(array);
- else if(ch=='c'){
- stsrt(ptstr, count);
- for(k=0; k<count; k++)
- puts(ptstr[k]);
- }
- else if(ch=='d')
- firstword(array);
- }
- */
- printlist(array);
- stsrt(ptstr, count);
- for(k=0; k<count; k++)
- puts(ptstr[k]);
- firstword(array);
- return 0;
- }
- void printlist(char array[LIM][SIZE]){
- int x;
- for(x=0; x<count; x++){
- printf("%d.%s\n", x+1, array[x]);
- }
- }
- void firstword(char array[LIM][SIZE]){//doesn't work at all
- int x, y=0;
- int first_len=0;
- for(x=0; x<count; x++){
- if((y+1)==(int)strlen(array[x])){
- printf("hi\n");
- first_len=(int)strlen(array[x]);
- }
- else{
- while(!isspace(array[x][y])){
- y++;
- first_len++;
- printf("length is now %d\n", first_len);
- }
- }
- printf("%s\n", array[x]);
- printf("%d\n", first_len);
- first_len=0;
- }
- }
- void stsrt(char *strings[], int num){//sort strings by length
- char *temp;
- int top, seek;
- for(top=0; top<num-1; top++){
- for(seek=top+1; seek<num; seek++){
- if((strlen(strings[seek]))<(strlen(strings[top]))){
- temp=strings[top];
- strings[top]=strings[seek];
- strings[seek]=temp;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment