Thread: Sort Characters in File

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    5

    Exclamation Sort Characters in File

    File content :
    a
    x
    z
    u
    e
    f
    g
    h
    k
    p
    r
    o
    p
    q
    r
    s
    k
    h
    g
    e
    l
    e
    t
    read the program up to the last character of the character string and use the program to find the longest consecutive character sequence in alphabetical order
    sample character string : x,y,z or e,f,g,h etc.


    I just could not make the order, please help

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    So what's your latest effort?

    Saying "you tried" and hoping someone will just post a working answer just isn't how this works.

    I mean, could you tell if 'ab' is a sequence and 'ac' isn't?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    5



    stack structure and read from file. for example ; afgbecwfgh ranking : abc , efgh , w






  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    5

    Code:
     #include <stdio.h>
    Code:
    #include <string.h> 
    #include <stdlib.h> 
    #define MAXLENGTH 30 
    
    typedef struct node { 
    char word[MAXLENGTH]; 
    }DATA; 
    
    typedef struct stack { 
    DATA data[MAXLENGTH]; 
    int top; 
    }Stack; 
    
    void write(FILE *filee,Stack* st) { 
    while(!feof(filee)) { 
    st->top++; 
    fgets(st->data[st->top].harf,MAXLENGTH,filee); 
    //printf("%s",st->data[st->top].word); 
    } 
    } 
    
    void read(Stack* st) { 
    FILE *filee; 
    filee=fopen("example.txt","r"); 
    write(filee,st); 
    } 
    
    int main() { 
    int i; 
    Stack st; 
    st.top=-1; 
    
    read(&st); 
    
    for(i=0;i<=st.top;i++) { 
    printf("%s\n",st.data.word); 
    
    } 
    
    
    } 
    

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Pop two characters from your stack and see if they're sequential... if they are, you have a sequence of 2, and now you can build the sequence by popping elements.
    Once you pop an element that is not in sequence, print the old sequence and it's length. Keep the current element as the start of a new sequence.

    Once you can do that I think you'll figure out how to remember which one is the longest you've found, maybe by using a spare stack.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,523
    Why is the title of this thread "sort"? Sorting is not required at all

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    5
    I don't understand , Please tell me by code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file of ints, sort it and write to another file
    By telmo_d in forum C Programming
    Replies: 6
    Last Post: 04-21-2015, 07:36 AM
  2. Radix Sort ( + counting sort) does not stable sort.
    By siepet in forum C Programming
    Replies: 6
    Last Post: 11-26-2013, 12:04 PM
  3. Replies: 1
    Last Post: 07-30-2011, 12:49 PM
  4. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 09:19 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM

Tags for this Thread