Thread: Find Error in my Code(Sorting Strings)

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    15

    Find Error in my Code(Sorting Strings)

    I want to stop repetition and print a word only once..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
        printf("\n\t\t\tSorting Strings\n");
        int x = -1,m;
        char str[100][100],temp[100];
        FILE * in = fopen("input.txt","r");
        do{
            fscanf(in,"%s",str[++x]);
        }while(strcmp(str[x],"end")!=0);
    
    
        for(int i=0;strcmp(str[i],"end")!=0;i++)
                   strlwr(str[i]);
    
    
     for(int i=0;strcmp(str[i],"end")!=0;i++)
        {
            for(m=i-1;m>=0;m--)
            {
            if(str[i]==str[m])
                       break;
            }
            if(str[i]==str[m] && m>=0)
                       continue;
    
    
        for(int j=i+1;strcmp(str[j],"end")!=0;j++)
            {
                if(strcmp(str[i],str[j])==1)
                {
                    strcpy(temp,str[i]);
                    strcpy(str[i],str[j]);
                    strcpy(str[j],temp);
                }
            }
            printf("\n\t%s",str[i]);
        }
        printf("\n\n");
        return 0;
    }
    Input:
    f you can dream and not make dreams your master
    If you can think and not make thoughts your aim
    If you can meet with Triumph and Disaster
    And treat those two imposters just the same
    If you can fill the unforgiving minute
    With sixty seconds worth of distance run
    Yours is the Earth end
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well the first problem with your sorting loop is that "end" won't always be at the end.

    Second, you try and print inside your sort function.

    A development process

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int readfile(str[100][100], const char *filename) {
        FILE * in = fopen(filename,"r");
        int n = 0;
        while ( fscanf(in,"%s",str[n] == 1 ) {
            n++;
        }
        fclose(in);
        return n;
    }
    
    void lowercase(str[100][100], int numwords ) {
        for(int i=0;i < numwords;i++)
                   strlwr(str[i]);
    }
    
    void sortwords(str[100][100], int numwords ) {
      for ( int i = 0 ; i < numwords ; i++ ) {
        for(int j=i+1;j < numwords ;j++)
            {
                if(strcmp(str[i],str[j])>0) // it's not specifically == 1
                {
                    char temp[100];
                    strcpy(temp,str[i]);
                    strcpy(str[i],str[j]);
                    strcpy(str[j],temp);
                }
            }
        }
      }
    }
    
    int main()
    {
        printf("\n\t\t\tSorting Strings\n");
        char str[100][100];
    
        int numwords = readfile(str,"input.txt"); 
        // printwords(str, numwords); // use this to make sure the file read OK
    
        lowercase(str, numwords);
        // printwords(str, numwords); // use this to check ...
     
        sortwords(str, numwords);
        // printwords(str, numwords); // use this to check ...
    
        numwords = removedups(str, numwords);
    
        printwords(str, numwords);
        return 0;
    }
    Once the words are sorted, all the duplicate entries will be adjacent in the sorted array.

    So you don't have to do complex searches for them, which makes them easier to deal with.

    Have a go at writing your own removedups() and printwords() functions.
    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
    Dec 2020
    Posts
    15
    I still didn't get how to delete duplicate entries... I am a Beginner..
    XrossHAIR

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start with a practical experiment.

    - grab a few coins and arrange into denomination order.
    or
    - write 1, 2, 2, 3, 3, 3, 4, 5, 5, 6 on some small bits of paper and arrange into order.

    Now, pretend you're a computer with simple capabilities like being able to point at elements of your data and make comparisons.

    Figure out what it means to 'remove duplicates', and only then can you write the code.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2020
    Posts
    15
    I tried this...Yet i Failed..XrossHair
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int removedups(char str[100][100],int num)
    {
       for(int i=0;str[i][0]!='\0';i++)
       {    if(str[i]==str[i+1])
       {
           strcpy(str[i+1]," ");
           --num;
       }
       }
       return num;
    }
    
    
    void printwords(char str[100][100],int num)
    {
        for(int i=0;i<num;i++)
            printf("\n%s",str[i]);
    }
    
    
    int readfile(char str[100][100], const char *filename) {
        FILE * in = fopen(filename,"r");
        int n = 0;
        while ( fscanf(in,"%s",str[n]) == 1 ) {
            n++;
        }
        fclose(in);
        return n;
    }
    
    
    void lowercase(char str[100][100], int numwords ) {
        for(int i=0;i < numwords;i++)
                   strlwr(str[i]);
    }
    
    
    void sortwords(char str[100][100], int numwords ) {
      for ( int i = 0 ; i < numwords ; i++ ) {
        for(int j=i+1;j < numwords ;j++)
            {
                if(strcmp(str[i],str[j])>0) // it's not specifically == 1
                {
                    char temp[100];
                    strcpy(temp,str[i]);
                    strcpy(str[i],str[j]);
                    strcpy(str[j],temp);
                }
            }
        }
      }
    
    
    
    
    int main()
    {
        printf("\n\t\t\tSorting Strings\n");
        char str[100][100];
    
    
        int numwords = readfile(str,"input.txt");
        // printwords(str, numwords); // use this to make sure the file read OK
    
    
        lowercase(str, numwords);
        // printwords(str, numwords); // use this to check ...
    
    
        sortwords(str, numwords);
        // printwords(str, numwords); // use this to check ...
    
    
        numwords = removedups(str, numwords);
    
    
        printwords(str, numwords);
        return 0;
    }
    Thanks For the previous Reply

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I tried this...Yet i Failed..XrossHair
    Yeah, in 13 minutes max.

    Excluding notification time, reading time and coding time, you couldn't have spent more than 5 minutes thinking about your first attempt.

    Try again, and don't give up so easily.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to start with something even simpler: declare and initialise an array of integers, then have your program remove the duplicates and print the resulting array.

    If you still fail, post that program and explain how does it not work. Don't just say "yet I failed".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2020
    Posts
    15
    I finally got it... Thanks Everyone...XrossHAIR

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can not find error in this code
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 11:04 AM
  2. Can't Find Error in Code
    By DarkDot in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2007, 09:56 PM
  3. Can't find error in code
    By blackgingr in forum C Programming
    Replies: 10
    Last Post: 11-14-2002, 09:00 PM
  4. sorting array efficiently...can't find the error
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-09-2002, 02:32 PM
  5. HELP! can't find that one error in my code
    By andyt2000 in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2001, 09:25 PM

Tags for this Thread